我在 UDP 客户端下编写了它,它基本上生成了一个单独的线程来接收数据报,但是数据报仅在主线程中发送。现在,按下 ctrl^D 在 Linux 发行版上实例化 ("./udpClient 1") UDP 客户端后,实现退出循环(围绕 getline () 调用)并关闭套接字描述符。
我观察到,尽管在描述符上调用了 close ,但直到客户端进程终止并且仍然在执行“netstat -an|grep udp”时看到相关条目时才会释放它。但是,如果我注释/删除生成接收器线程的代码,则套接字描述符会在调用 close() 后立即释放。
似乎套接字引用计数在生成一个单独的线程(它基本上接收并阻塞所述套接字描述符)时增加,因此 close () 调用什么也不做。
这是理想的吗,因为我理解如果套接字描述符在进程之间而不是在线程之间共享,套接字引用计数会增加?是因为 Linux 将线程实现为 LWP,如果是这样,这是期望的行为吗?
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
void *receiver (void *arg);
int sockfd;
int doConnect = 0;
int
main (int argc, char *argv[])
{
pthread_t thrid;
struct sockaddr_in servaddr;
struct sockaddr_in clntaddr;
char *line;
size_t len = 0;
size_t read;
if (argc < 4)
{
printf
("Usage:%s <Server Ipv4 address> <Server Port> <Connect Yes(1)/No(0)> [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_DGRAM, IPPROTO_UDP)) == -1)
{
printf ("Failed to create socket: %s\n", strerror (errno));
return -1;
}
bzero (&clntaddr, sizeof (struct sockaddr_in));
if (argc == 6)
{
printf ("doing bind......\n");
clntaddr.sin_family = AF_INET;
clntaddr.sin_port = htons (atoi (argv[5]));
inet_aton (argv[4], &clntaddr.sin_addr);
if (bind
(sockfd, (struct sockaddr *) &clntaddr,
sizeof (struct sockaddr_in)) == -1)
{
printf ("Failed to bind: %s\n", strerror (errno));
close (sockfd);
return -1;
}
}
/*
* fill up the server details
*/
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);
doConnect = atoi (argv[3]);
if (1 == doConnect)
{
printf ("doing connect......\n");
if (-1 ==
connect (sockfd, (struct sockaddr *) &servaddr,
sizeof (struct sockaddr_in)))
{
printf ("Failed to connect: %s\n", strerror (errno));
close (sockfd);
return -1;
}
}
if (pthread_create (&thrid, NULL, receiver, NULL) < 0)
{
printf ("Failed to create thread: %s\n", strerror (errno));
close (sockfd);
return -1;
}
while ((read = getline (&line, &len, stdin)) != -1)
{
if (1 == doConnect)
{
if (send (sockfd, line, len, 0) < 0)
{
printf ("send () failed: %s\n", strerror (errno));
}
}
else
{
if (sendto (sockfd, line, len, 0, (struct sockaddr *) &servaddr,
sizeof (struct sockaddr_in)) < 0)
{
printf ("sendto () failed: %s\n", strerror (errno));
}
}
}
if (line)
free (line);
close (sockfd);
printf ("socket is closed.....\n");
sleep (60);
}
void *
receiver (void *arg)
{
char buff[512];
while (1)
{
memset (buff, 0, sizeof (buff));
if (1 == doConnect)
{
if (recv (sockfd, buff, sizeof (buff), 0) < 0)
{
// printf ("recvfrom () failed: %s\n", strerror (errno));
printf ("recv () failed\n");
}
else
{
printf ("recv () returned: %s\n", buff);
}
}
else
{
if (recvfrom (sockfd, buff, sizeof (buff), 0, NULL, NULL) < 0)
{
// printf ("recvfrom () failed: %s\n", strerror (errno));
printf ("recvfrom () failed\n");
}
else
{
printf ("recvfrom () returned: %s\n", buff);
}
}
}
}