[英]TCP FIN not sent on doing 'close ()' for a multi-threaded TCP client
I have written below multi-threaded TCP client which basically spawns a separate thread for receiving the data however the data is being written in the main thread only having taken input from the user on standard input. 我在下面编写了多线程TCP客户端,该客户端基本上产生了一个单独的线程来接收数据,但是仅在从用户输入标准输入的情况下,才将数据写入主线程。
Now, having pressed ctrl^D then implementation comes out of the loop (around the getline () call) and closes the socket descriptor but no FIN is seen on the wire. 现在,按下ctrl ^ D之后,实现就会退出循环(在getline()调用周围)并关闭套接字描述符,但在线上看不到FIN。 However, replacing close() with shutdown () makes a difference.
但是,用shutdown()替换close()会有所不同。 With close() no FIN is sent on the wire but with shutdown(fd, SHUT_WR) FIN is sent on the wire?
使用close()时,没有在网络上发送FIN,但是使用shutdown(fd,SHUT_WR)时,在网络上发送了FIN?
Why is this difference? 为什么会有这种差异?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
void *receiver (void *arg);
pthread_t thrid;
int sockfd;
int reUse = 0;
int
main (int argc, char *argv[])
{
struct sockaddr_in servaddr;
struct sockaddr_in clntaddr;
char *line;
size_t len = 0;
size_t read;
int bytes;
if (argc < 6)
{
printf
("Usage:%s <Server Ipv4 address> <Server Port> <SO_REUSEADDR Yes(1)/No(0))> <Close(0)/SHUT_RD(1)/SHUT_WR(2)/SHUT_RDWR(3)> <sleep (in sec)> [Client IPv4 address] [Client Port]\n",
argv[0]);
return -1;
}
/*
* AF_INET, AF_INET6, AF_UNIX, AF_NETLINK
* SOCK_STREAM (TCP), SOCK_DGRAM (UDP), SOCK_RAW
*/
if ((sockfd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{
printf ("Failed to create socket: %s\n", strerror (errno));
return -1;
}
/*
* set SO_REUSEADDR option for the UDP server socket
*/
reUse = atoi (argv[3]);
if (reUse)
{
int i = 1;
setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR, (void *) &i,
sizeof (i));
}
bzero (&clntaddr, sizeof (struct sockaddr_in));
if (argc == 8)
{
printf ("doing bind......\n");
clntaddr.sin_family = AF_INET;
clntaddr.sin_port = htons (atoi (argv[7]));
inet_aton (argv[6], &clntaddr.sin_addr);
if (bind
(sockfd, (struct sockaddr *) &clntaddr,
sizeof (struct sockaddr_in)) == -1)
{
perror ("Failed to bind");
close (sockfd);
return -1;
}
}
bzero (&servaddr, sizeof (struct sockaddr_in));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons (atoi (argv[2]));
inet_aton (argv[1], &servaddr.sin_addr);
if (-1 ==
connect (sockfd, (struct sockaddr *) &servaddr,
sizeof (struct sockaddr_in)))
{
printf ("Failed to connect: %s\n", strerror (errno));
return -1;
}
if (pthread_create (&thrid, NULL, receiver, NULL) < 0)
{
printf ("Failed to create thread\n");
}
while ((read = getline (&line, &len, stdin)) != -1)
{
bytes = send (sockfd, line, len, 0);
if (bytes < 0)
{
if (errno == EINTR)
continue;
else
printf ("%Read error %s\n", pthread_self (),
strerror (errno));
}
}
if (0 == atoi (argv[4]))
{
printf ("doing close()....\n");
close (sockfd);
}
else if (1 == atoi (argv[4]))
{
printf ("doing shutdown(..., SHUTRD)....\n");
shutdown (sockfd, SHUT_RD);
}
else if (2 == atoi (argv[4]))
{
printf ("doing shutdown(..., SHUTWR)....\n");
shutdown (sockfd, SHUT_WR);
}
else if (3 == atoi (argv[4]))
{
printf ("doing shutdown(..., SHUTRDWR)....\n");
shutdown (sockfd, SHUT_RDWR);
}
if (line)
free (line);
sleep (atoi (argv[5]));
}
void *
receiver (void *arg)
{
char buff[512];
int bytes;
while (1)
{
bytes = recv (sockfd, buff, sizeof (buff), 0);
if (bytes < 0)
{
if (errno == EINTR)
continue;
else
printf ("%Read error %s\n", pthread_self (),
strerror (errno));
pthread_exit (-1);
}
else if (bytes == 0)
{
printf ("connection closed by Peer\n");
close(sockfd);
pthread_exit (-1);
}
else
{
printf ("Msg Received:%s\n", buff);
}
}
}
调用shutdown(WR)将发送FIN,但关闭有一点不同:如果fd参考计数不等于0,则不会发送FIN。
您可以在关闭(套接字)之前使用shutdown(套接字,SHUT_WR)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.