| | |
| | | { |
| | | //本地Socket |
| | | public static Socket busSocket; |
| | | /// <summary>
|
| | | /// 计时器
|
| | | /// </summary> |
| | | private static int timeCount = 0; |
| | | |
| | | /// <summary> |
| | | /// 启动Socket接收和发送功能 |
| | |
| | | { |
| | | try |
| | | { |
| | | //检测连接状态 |
| | | CheckConnectiton(); |
| | | |
| | | Start(7624); |
| | | busSocket.BeginSendTo(bytes, 0, bytes.Length, SocketFlags.None, iPEndPoint, new AsyncCallback(asyncEndSend), null); |
| | | } |
| | | catch { } |
| | | } |
| | | |
| | | /// <summary>
|
| | | /// 检测连接状态
|
| | | /// </summary> |
| | | private static void CheckConnectiton()
|
| | | {
|
| | | if (busSocket == null) { return; }
|
| | | timeCount++;
|
| | | if (timeCount < 20)
|
| | | {
|
| | | //每10秒检测一次
|
| | | return;
|
| | | }
|
| | | timeCount = 0;
|
| | |
|
| | | bool blockingState = busSocket.Blocking;
|
| | | try
|
| | | {
|
| | | byte[] tmp = new byte[1];
|
| | |
|
| | | busSocket.Blocking = false;
|
| | | busSocket.Send(tmp, 0, 0);
|
| | | //tcp还在连接着
|
| | | busSocket.Blocking = blockingState;
|
| | | }
|
| | | catch (SocketException e)
|
| | | {
|
| | | // 10035 == WSAEWOULDBLOCK
|
| | | if (e.NativeErrorCode.Equals(10035))
|
| | | {
|
| | | //Still Connected, but the Send would block
|
| | | }
|
| | | else
|
| | | {
|
| | | //tcp已经断开了连接
|
| | | Stop();
|
| | | }
|
| | | }
|
| | | } |
| | | |
| | | /// <summary> |