最近工作中涉汲到一些Socket 方面应用 ,如断线重连,连接状态判断等,今天做了一些总结。
1.判断Socket 连接状态
通过 Poll 与 Connected 结合使用 ,重点关注 SelectRead 模式
方法名:
Socket.Poll (int microSeconds, System.Net.Sockets.SelectMode mode) 方法参数:
参数枚举:
SelectRead如果已调用 Listen(Int32) 并且有挂起的连接,则为 true。 - 或 - 如果有数据可供读取,则为 true。 - 或 - 如果连接已关闭、重置或终止,则返回 true; 否则,返回 false。
SelectWrite如果正在处理 Connect(EndPoint) 并且连接已成功,则为 true; - 或 - 如果可以发送数据,则返回 true; 否则,返回 false。
SelectError如果正在处理不阻止的 Connect(EndPoint),并且连接已失败,则为 true; - 或 - 如果 OutOfBandInline 未设置,并且带外数据可用,则为 true; 否则,返回 false。
SelectRead 模式返回 true 的三种情况,若Socket 处挂起状态 ,同时没有数据可读取,可以确定Socket 是关闭或终止的。
1.已调用 Listen(Int32) 并且有挂起的连接,则为 true
2.有数据可供读取,则为 true
3.如果连接已关闭、重置或终止,则返回 true
如下代码 ,true 表示连接断开
s.Poll(1000, SelectMode.SelectRead) && (s.Available == 0) || !s.Connected
稍作调整,判断连接状态完整代码如下:
public static boolIsSocketConnected(Socket s)
{#region remarks
/*As zendar wrote, it is nice to use the Socket.Poll and Socket.Available, but you need to take into consideration
* that the socket might not have been initialized in the first place.
* This is the last (I believe) piece of information and it is supplied by the Socket.Connected property.<