c#判断socket是否还连接着
2012-07-25 15:39
393 查看
刚开始用的是Socket.Connected。但是,msdn上说:“
The Connected property gets the connection state of the Socket as of the last I/O operation. When it returns false, the Socket was either never connected, or is no longer connected.
The value of the Connected property reflects the state of the connection as of the most recent operation. If you need to determine the current state of the connection, make a nonblocking, zero-byte Send call. If the call returns successfully or throws
a WAEWOULDBLOCK error code (10035), then the socket is still connected; otherwise, the socket is no longer connected.
”
所以不能用Socket.Connected判断。
使用方法:
The Connected property gets the connection state of the Socket as of the last I/O operation. When it returns false, the Socket was either never connected, or is no longer connected.
The value of the Connected property reflects the state of the connection as of the most recent operation. If you need to determine the current state of the connection, make a nonblocking, zero-byte Send call. If the call returns successfully or throws
a WAEWOULDBLOCK error code (10035), then the socket is still connected; otherwise, the socket is no longer connected.
”
所以不能用Socket.Connected判断。
使用方法:
private bool SocketConnected() { try { return !_socket.Poll(1, SelectMode.SelectRead) && (_socket.Available == 0); } catch (SocketException) { return false; } catch (ObjectDisposedException) { return false; } }
相关文章推荐
- C#使用Socket快速判断数据库连接是否正常的方法
- C#怎么判断一个Socket是否连接
- C#怎么判断一个Socket是否连接
- C#怎么判断一个Socket是否连接
- C#怎么判断一个Socket是否连接
- C#怎么判断一个Socket是否连接
- c#通过socket判断服务器连接是否正常
- C# 判断是否可以连接服务器?
- C#判断连接是否可下载
- java中判断socket网络远端是否断开连接
- java中判断socket服务器端是否断开连接
- C#判断是否存在已连接的真实网卡(NetConnectionStatus)
- java中判断socket网络远端是否断开连接
- java中判断socket网络远端是否断开连接
- C#判断检测网络是否连接
- 正常断开连接情况下,判断非阻塞模式socket连接是否断开
- 【C#源码】判断网络是否连接上
- c# 判断网络是否连接
- socket 精确的判断出网络是否连接
- 如何判断socket是否连接,异常断开拔出网线