.NET 2.0 WINFORM 。一个socket 服务开启监听后,有很多的客户端连接。使用一段时间后,会出现某个客户端连接不上,重启客户端也不行。除非关掉服务端,在开启服务,那个连接不上的客户端才能连接上。大神们,能帮我分析下原因吗?服务端和客户端都是异步。

解决方案 »

  1.   

    检查一下服务端的socket连接等相关的资源有没有释放掉。
      

  2.   

    服务端的资源有没有释放掉,和你客户端重启与否有关系吗?
    比如通过socket建立连接之后,客户端重启,而服务端与客户端保持连接的资源没有释放。
      

  3.   

       public void OnRecievedData(IAsyncResult ar)
            {
                try
                {
                    SocketServerClient client = (SocketServerClient)ar.AsyncState;
                    byte[] aryRet = client.GetRecievedData(ar);
                    if (aryRet.Length < 1)
                    {
                        client.Sock.Close();
                        this.my_ClientList.Remove(client);
                    }
                    else
                    {
                        string data = Encoding.GetEncoding("GBK").GetString(aryRet);
                        this.OnReceivedString(this, new ReceivedMessageEventArgs(data, client, this.my_ClientList));
                        client.SetupRecieveCallback(this);
                    }
                }
                catch { }
            }客户端 下线后,服务端会收到 “0” ,上面的代码就回收了
      

  4.   

    我把服务器端代码贴出来 ,麻烦大家帮我看看 有没有问题 
     public class ServerSocked
        {
            // Fields        private Socket listener;
            public ArrayList my_ClientList = new ArrayList();
            public bool isRun = true;
            // Events
            public event ReceiveMessageHandler receiveddata;        // Methods
            public void closeSocket()
            {
                try
                {
                    this.isRun = false;
                    this.listener.Close();
                    foreach (SocketServerClient clientSend in this.my_ClientList)
                    {
                        clientSend.Sock.Close();
                        this.my_ClientList.Remove(clientSend);
                        //return;
                    }
                }
                catch
                {
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            /// <summary>
            /// 建立新连接
            /// </summary>
            /// <param name="sockClient"></param>
            public void NewConnection(Socket sockClient)
            {
                try
                {
                    SocketServerClient client = new SocketServerClient(sockClient);
                    this.my_ClientList.Add(client);
                    client.socketname = client.Sock.RemoteEndPoint.ToString();
                    client.SetupRecieveCallback(this);
                }
                catch { }
            }        public void OnConnectRequest(IAsyncResult ar)
            {
                try
                {
                    // if (this.isRun)
                    // {
                    Socket listener = (Socket)ar.AsyncState;
                    this.NewConnection(listener.EndAccept(ar));
                    listener.BeginAccept(new AsyncCallback(this.OnConnectRequest), listener);
                    //  }
                }
                catch
                { }
            }        public void OnReceivedString(object sender, ReceivedMessageEventArgs e)
            {
                try
                {
                    if (this.receiveddata != null)
                    {
                        this.receiveddata(sender, e);
                    }
                }
                catch { }
            }        public void OnRecievedData(IAsyncResult ar)
            {
                try
                {
                    SocketServerClient client = (SocketServerClient)ar.AsyncState;
                    byte[] aryRet = client.GetRecievedData(ar);
                    if (aryRet.Length < 1)
                    {
                        client.Sock.Close();
                        this.my_ClientList.Remove(client);
                    }
                    else
                    {
                        string data = Encoding.GetEncoding("GBK").GetString(aryRet);
                        this.OnReceivedString(this, new ReceivedMessageEventArgs(data, client, this.my_ClientList));
                        client.SetupRecieveCallback(this);
                    }
                }
                catch { }
            }
            /// <summary>
            /// 开始监听
            /// </summary>
            /// <param name="nPortListen"></param>
            /// <param name="ip"></param>
            public void startSocket(int nPortListen, string ip)
            {
                try
                {
                    this.isRun = true;
                    IPAddress ipAddr = IPAddress.Parse(ip);
                    this.listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    this.listener.Bind(new IPEndPoint(ipAddr, nPortListen));
                    this.listener.Listen(1024);
                    this.listener.BeginAccept(new AsyncCallback(this.OnConnectRequest), this.listener);
                }
                catch
                {
                    this.isRun = false;
                    //throw new ApplicationException("socket start error");
                }
            }        // Nested Types
            public delegate void ReceiveMessageHandler(object sender, ReceivedMessageEventArgs e);
        }
      

  5.   

    客户端和服务端 有没有保持心跳连接?你可以参考下腾讯的QQ 处理方式 每台电脑的QQ过2分钟都会向服务器发送一个请求。如果服务器接收不到客户端发送的请求默认 客户端已经掉线关掉连接请求。
    TCP 还是UDP 都需要注意适时释放链接请求。
      

  6.   

    做这个项目的人也是脑残 谁开发的啊
    不用wcf脑残脑残脑残啊
    自己写Socket 你对效率要求就这么高啊
      

  7.   

    SocketAsyncEventArgs 保准解决后顾之忧
      

  8.   


    http://technet.microsoft.com/zh-cn/library/system.net.sockets.socketasynceventargs(v=vs.90).aspx已经封装好了 包括异步套接字池,自己只需要实现具体接收数据后处理的方法