一端的套接字上的close()是否也会在另一端关闭吗? [英] Does close() on socket on one end, closes on the other end as well?

查看:66
本文介绍了一端的套接字上的close()是否也会在另一端关闭吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果ESTABLISHED套接字(通过 connect()从客户端连接后)退出,则内核关闭所有打开的文件描述符,那么另一端会发生什么?如果客户端发送FIN并由服务器对其进行确认(这只是半关闭状态),但是服务器尝试在该套接字上进行 read(),那么会发生什么呢?我可以想象2种情况:

If ESTABLISHED socket (after connecting from client via connect()), exits and thus kernel closes all open file descriptor, what happens to the other side? If the client sends FIN and the server ACK it (which is just half-closed state), but the server tries to read() on that socket, what happen then? I can imagine 2 situation:

  1. 套接字服务器 read()处于打开状态,也已关闭.但是在服务器端没有exit(),因此没有人在该端关闭该套接字.因此,在这里我不知道服务器如何结束,因为不应关闭该套接字的末端

  1. the socket server read()s on, is closed also. But in the server side there is no exit(), so noone has closed that socket at that side. So here I do not know how server ends, since its end of that socket should not be closed

被关闭的服务器未关闭,但它读取0个字节.(来自 read()的返回值就是 0 ),其余内容仍保留在设计器中如何处理读取的返回值.但是,即使服务器端套接字未关闭,服务器何时仍会发送 FIN位?完成执行后(完成完整的连接终止)?

the server socked is not closed, but it reads 0 bytes. (the return value from read() is simply 0) and the rest remain on designer how to handle return value from read. But still even if the server side socket is not closed, when does the server sends its FIN bit? After it finishes execution (to complete full connection termination) ?

这是服务器端从封闭套接字(在客户端封闭)中读取的语句:

here is the statement at server side that reads from closed socket (closed at client side):

while ((len = read(sockfd, buf, 256)) > 0){
...
}

在这里,由于 read()在封闭的 sockfd 上读取,它会返回吗?还是因为 read()返回0,从而使条件为假?(上述2种情况).据我所知,如果 read()将在封闭的fd上读取,则错误将返回(-1).但是0字节的读取只是返回(0).那么返回了什么?

here, will it return because the read() reads on closed sockfd? or because the read() returns 0 and thus false the condition? (the 2 situation described above). As far as I know, if read() would read on closed fd, then error would be return (-1). But 0 bytes reads just return (0). So what is returned?

推荐答案

关闭连接意味着两个对等点都同意他们不希望彼此之间再进行通信.如果只有一个对等方关闭套接字,则它仅与FIN通信,它将不再发送任何数据.它还向本地操作系统传达它不再愿意接收任何数据的信息-这里的 close(sock) shutdown(sock,SHUT_WR)不同.

A close of a connection means that both peers agree that they don't want to communicate any more with each other. If only one peer closes the socket it just communicates with the FIN that it will no longer send any data. It also communicates to the local OS that it is no longer willing to receive any data - here close(sock) differs from shutdown(sock,SHUT_WR).

如果客户端关闭或关闭套接字,对服务器中 read 的调用将返回0,因为这意味着不再有更多数据从客户端发送到服务器.然后,服务器可能决定也关闭或关闭套接字.但由于套接字尚未在服务器中关闭,因此可能还会决定向客户端发送更多数据.如果服务器发送更多数据,则客户端将以RST(连接重置)进行响应,因为它不希望收到更多数据.收到RST时,服务器端套接字也会自动关闭.

A call to read in the server will return 0 if the client closed or shutdown the socket, since this meant that no more data are send from client to server. The server then might decide to also close or shutdown the socket. But might also decide to send more data to the client, since the socket is not closed yet in the server. If the server sends more data the client will respond with a RST (connection reset) since it is not expecting any more data. When receiving the RST the server side socket gets automatically closed too.

while ((len = read(sockfd, buf, 256)) > 0){

在大多数情况下,如果客户端已关闭连接,则 read 将在此处返回0.万一套接字发生错误(特别是连接重置),它将返回-1.如果在客户端已经关闭连接的同时服务器已向客户端写入数据(即竞争条件),则可能会发生这种情况,在这种情况下,客户端将返回RST.该错误将在套接字的下一个系统调用中传递,即 read .

In most cases read will return 0 here if the client has closed the connection. It will return -1 in case an error on the socket occurred, notably Connection reset. This might happen if the server has written data to the client while the client has already closed the connection (i.e. race condition), in which case the client will return with a RST. This error will be delivered on the next syscall on the socket, i.e. the read.

这篇关于一端的套接字上的close()是否也会在另一端关闭吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆