码迷,mamicode.com
首页 > 其他好文 > 详细

close vs shutdown socket

时间:2016-08-22 21:25:59      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

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

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

1、shutdown会发FIN  完成4步挥手,close 发RST。由此可见一个温柔一个粗鲁。

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.

 

小程序验证

close vs shutdown socket

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!