C语言 关闭客户端套接字并保持服务器套接字处于活动状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27798419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
closing client socket and keeping server socket active
提问by Sathiya
I am establishing a server-client connection using TCP sockets. Whenever I close the client socket my server is also closed. But I want only my client to be closed and my server must wait for next accept().
我正在使用 TCP 套接字建立服务器-客户端连接。每当我关闭客户端套接字时,我的服务器也会关闭。但我只想关闭我的客户端,而我的服务器必须等待 next accept()。
Server side:
服务器端:
{
bind(lfd,(struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(lfd, 10);
while(1)
{
cfd = accept(lfd, (struct sockaddr*)NULL, NULL);
//server must wait here after client closes the connection application code
close(lfd);
}
}
client side:
客户端:
inet_pton(AF_INET, argv[1], &serv_addr.sin_addr);
connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
// ... application code
if(c == 1)
close(fd);
回答by abligh
When you acceptserver side, you generate a new socket for that client only.
当您在accept服务器端时,您仅为该客户端生成一个新套接字。
When you have finished dealing with the client you mustclose()that socket, (that's close(cfd)in your terminology). You mayalso shutdown()the socket - that will influence how the socket is closed at a TCP level. But whether you do or do not do a shutdown(), you mustclose()it, else you will leak FDs.
当您完成与客户端的处理后,您必须使用close()该套接字(这是close(cfd)您的术语)。您也可以shutdown()使用套接字 - 这将影响套接字在 TCP 级别的关闭方式。但是无论你做还是不做shutdown(),你都必须close()这样做,否则你会泄露FD。
You must not close()your listen fd (lfdin your program) until you intend not to accept any more connections.
除非您打算不再接受任何连接,否则close()您不能监听 fd(lfd在您的程序中)。
TLDR: change close(lfd)to close(cfd)
TLDR:更改close(lfd)为close(cfd)
回答by Manos
The TCP listening socket described by the descriptor lfdis used for waiting for TCP incoming connections at a specific port. After the call of accepta new socket descriptor is created, the cfdin your example.
描述符描述的 TCP 侦听套接字lfd用于等待特定端口的 TCP 传入连接。accept创建新套接字描述符的调用后,cfd在您的示例中。
All the data exchange between server and client is performed using the cfd. If the client first close the socket, a possible sendor recvat the server side, will return -1 with the appropriate errnovalue.
服务器和客户端之间的所有数据交换都使用cfd. 如果客户端首先关闭套接字,a 可能send或recv在服务器端,将返回 -1 和适当的errno值。
If you want the server to close the connection, you should use shutdown(cfd, SHUT_RDWR)and close(cfd)after, NOT close(lfd). This lets the lfdsocket open, allowing the server to wait at the acceptfor the next incoming connection. The lfdshould close at the termination of the server.
如果您希望服务器关闭连接,您应该使用shutdown(cfd, SHUT_RDWR)and close(cfd)after,而不是close(lfd). 这让lfd套接字打开,允许服务器等待accept下一个传入连接。本lfd应关闭的服务器终止。
The shutdown()provides more flexibility, to send or receive remaining data prior the permanent termination of the communication.
这shutdown()提供了更大的灵活性,以在永久终止通信之前发送或接收剩余数据。
回答by lonewasp
accept call would return new socket descriptor (cfd in your code) that is connected to the client side, so when client closes its end of connection cfd will be closed and not lfd. You can use lfd (listening socket) for accepting connection as long as server needs. Also consider invoking shutdown before close(fd) in client code.
accept 调用将返回连接到客户端的新套接字描述符(代码中的 cfd),因此当客户端关闭其连接端 cfd 将关闭而不是 lfd。只要服务器需要,您就可以使用 lfd(侦听套接字)来接受连接。还可以考虑在客户端代码中的 close(fd) 之前调用 shutdown。

