研究生期间有一段时间就总被connection reset by peer折磨,昨天又遇到这种情况,郁闷,不得不搞清楚了
Shutting down a socket connection involves an exchange of protocol messages between the two endpoints, hereafter referred to as a shutdown sequence. Two general classes of shutdown sequences are defined: graceful and abortive (also called hard). In a graceful shutdown sequence, any data that has been queued, but not yet transmitted can be sent prior to the connection being closed. In an abortive shutdown, any unsent data is lost. The occurrence of a shutdown sequence (graceful or abortive) can also be used to provide an FD_CLOSE indication to the associated applications signifying that a shutdown is in progress.
意思就是说正常的流程是 data + FIN, 但也有可能是 data + rst + data, 后一种情况就是强制关闭,会导致客户端的connection reset by peer。
可以用linger这个选项来控制,setsockopt
但是,socket默认的情况就是如此,即会先发送数据,然后发送FIN,不会发送rst
所以,我遇到的问题不能用上面的方法来解决
我遇到的问题是,server没有读客户端发送的数据,而是直接发送数据,然后close,即在close的时候server的读缓存里还有数据
A B
send( ) data →
data →
data →
recv( ) →ERROR ← RST close( )
解决方法包括:
1. 将所有数据读出
2. 调用shutdown,然后调用close
相关文章链接:
http://cs.baylor.edu/~donahoo/practical/CSockets/TCPRST.pdf