0

In linux, how can i determine the state of a TCP socket? I searched many in this theme, but i does not finded real usable solution. I the most chases says: write something into the socket and it will fail if connection closed. But what if the content of stream is bound, and a may not write anything junk into the stream? I a other aspect the netstat can determine the socket state, but if i would like to implement the netstat based solution, it will be very expensive. I must resolv the fileDescriptor to socketID, than read all record from /proc/net/tcp, find the fitting line and test state is established. It is not so efficient if this method must be periodically called. It there some other shorthand solution for this problem?

4

2 回答 2

0

您必须执行套接字 I/O 操作以确定连接是否仍然存在。如果您无法写入套接字,您仍然可以尝试读取操作(只要它不干扰任何其他读取操作)并查看它是否因连接错误而失败。

于 2013-11-01T06:37:32.027 回答
0

你不能。TCP 套接字没有动态状态;它们只有先前操作的结果。如果您不在 TCP 连接上进行操作,则故意将其设计为不更改状态。它没有“拨号音”,这是它完全取代了像 SNA 这样的网络架构的根本原因。

于 2013-11-01T04:23:17.963 回答