zoukankan      html  css  js  c++  java
  • c# socket服务端判断连接状态

                    foreach (var dic in sockets)
                    {
                        if (dic.Value.Poll(1000, SelectMode.SelectRead))
                        {
                            dic.Value.Close();//关闭socket
                            sockets.TryRemove(dic.Key, out Socket socket);
    
                            _logger.Warn($"IP:{dic.Key},客户端socket连接断开;");
    
                            continue;
                        }
    
                        _logger.Info($"IP:{dic.Key},客户端socket连接正常;");
                    }

    private ConcurrentDictionary<string, Socket> sockets = new ConcurrentDictionary<string, Socket>();确保线程安全 

    主要是利用socket的poll方法来进行判断。

    另外我试了下使用KeepAlive,发现不起作用。但是如果放在客户端使用的话,就会起作用。不知道为什么。

    参考网址:https://www.cnblogs.com/cuisir/p/8522680.html

  • 相关阅读:
    spoj705
    bzoj2440
    spoj220
    bzoj2301
    hdu1695
    poj3294
    hdu3518
    poj3693
    函数
    样式
  • 原文地址:https://www.cnblogs.com/subendong/p/12524292.html
Copyright © 2011-2022 走看看