Home > other >  How to judge whether the connection status?
How to judge whether the connection status?

Time:09-21

For.net. Conn, and how to determine whether the connection has been closed? Before such as sending data to determine whether a connection can be used

CodePudding user response:

Net. Conn did not provide such interfaces to directly determine the
By using the abnormal capture mode

CodePudding user response:

Normal client close connections, you use err. EOF judgment, the server to get data length is zero, also think is closed, the server reads the timeout, also want to shut down, the client abnormal exit, you use the
ErrStr: err...=Error ()
If strings. The Contains (errStr, "An existing connection was forcibly closed by the remote host") | |
Strings. The Contains (errStr, "An established connection was aborted by the software in your host machine") | |
Strings. The Contains (errStr, "connection reset by peer") {
Break
}
This method although soil, good compatibility, Windows, Unix are available,

CodePudding user response:

TCP is operating common patterns, you when the connection is normal, a read or write, if the connection is disconnected, will return an error at this time,
  • Related