2010年12月4日 星期六

connect() raise No route to host and recv() raise Resource temporarily unavailable

當client socket 與 server 正常連線下, 中繼switch的網路線被短暫拔插時..
(1) close client socket
正常程序client socket當send(), 會偵測到網路已斷開, 此時須close()
(2) try to connect to server
正常程序client socket會試著建立新連線, 但由於switch須一段時間才會恢復,
connect()會引發ENETUNREACH (No route to host), 表示無法順利連線,
此時建議sleep()數秒, 再試著重新connect(), 直到成功建立連線
(3)send alive mssage and recv ack
正常程序client socket成功建立連線後, 會試著send() alive massage,
並recv() ack, 以表示雙方ap已確立建立連線..
若recv() with non-block, 猜測server此時重新binding socket,
recv()會引發EAGAIN (Resource temporarily unavailable),
表示sokcet無任何資料可以讀取, 建議捨棄此次alive massge作業,
sleep()數秒, 再進行一次send() alive message,
亦或是, sleep()1秒, 再進行recv() ack, 若重覆3次, 依舊無資料可讀取,
捨棄此次alive massage作業, sleep()數秒, 再進行一次send() alive message

man recv ::

If no messages are available at the socket, the receive calls wait for a message to arrive, unless the socket is nonblocking (see fcntl(2)), in which case the value -1 is returned and the external variable errno set to EAGAIN. The receive calls normally return any data available, up to the requested amount, rather than waiting for receipt of the full amount requested.

沒有留言: