close vs shutdown socket

http://stackoverflow.com/questions/4160347/close-vs-shutdown-socket

从网上的资料看总结一下几点:

1、shutdown会发FIN  完成4步挥手,close 发RST。由此可见一个温柔一个粗鲁。  验证并不是这样。都是发FIN。

2、shutdown单项关闭。不影响另一方向的读写。

shutdown is a flexible way to block communication in one or both directions. When the second parameter is SHUT_RDWR, it will block both sending and receiving (like close). However, close is the way to actually destroy a socket.With shutdown, you will still be able to receive pending data the peer already sent (thanks to Joey Adams for noting this).

The following haven't been tested, trust with your own risk. However, I believe this is a reasonable and practical way of doing things.

If the TCP stack receives a shutdown with SHUT_RD only, it shall mark this connection as no more data expected. Any pending and subsequent read requests (regardless which ever thread they are in) will then returned with zero sized result. However, the connection is still active and usable -- you can still receive OOB data, for example. Also, the OS will drop any data it receives for this connection. But that is all, no packages will be sent to the other side.

If the TCP stack receives a shutdown with SHUT_WR only, it shall mark this connection as no more data can be sent. All pending write requests will be finished, but subsequent write requests will fail. Furthermore, a FIN packet will be sent to another side to inform them we don't have more data to send.

小程序验证

原文地址:https://www.cnblogs.com/wocgcow/p/5796797.html