tcp rst packet

研究生期間有一段時間就總被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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章