首页 新闻 会员 周边 捐助

现在已经正在使用此 SocketAsyncEventArgs 实例进行异步套接字操作

0
悬赏园豆:50 [待解决问题]

       如题,我的程序在执行到 soc.ReceiveAsync(readEventArgsWithId.ReceiveSAEA);//侦听到客户端连接开始接受数据(重要)运行时会捕获到一个异常,不是所有时候都会报错,是运行一段时间后偶尔出现此现象,然后所有的客户端都连不上了。。请哪位高手指点一下。。。

 

       /// <summary>
        /// 接受连接
        /// </summary>
        /// <param name="acceptEventArg"></param>
        private void StartAccept(SocketAsyncEventArgs acceptEventArg)
        {
            try
            {
                if (serverstate != ServerState.Running)
                    return;

                if (acceptEventArg == null)
                {
                    acceptEventArg = new SocketAsyncEventArgs();
                    acceptEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(AcceptEventArg_Completed);
                }
                else
                {
                    acceptEventArg.AcceptSocket = null;
                }
                m_maxNumberAcceptedClients.WaitOne();
                if (listenSocket != null)
                {
                    Boolean willRaiseEvent = this.listenSocket.AcceptAsync(acceptEventArg);//接受连接异步(重要)
                    if (!willRaiseEvent)//同步
                    {
                        this.ProcessAccept(acceptEventArg);
                    }
                }
            }
            catch (Exception ex)
            {
                Err.Pro(ex);
            }
        }
        private void AcceptEventArg_Completed(object sender, SocketAsyncEventArgs e)
        {
            this.ProcessAccept(e);
        }
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            try
            {
                if (e.LastOperation != SocketAsyncOperation.Accept)    //检查上一次操作是否是Accept,不是就返回
                    return;

                string uid = GetIDByIP((e.AcceptSocket.RemoteEndPoint as IPEndPoint).Address.ToString());//根据IP获取用户的UID
                if (string.IsNullOrEmpty(uid))
                    return;
                if (readWritePool.BusyPoolContains(uid))//判断现在的用户是否已经连接,避免同一用户开两个连接
                    return;

                SocketAsyncEventArgsWithId readEventArgsWithId = this.readWritePool.Pop(uid);
                readEventArgsWithId.ReceiveSAEA.UserToken = e.AcceptSocket;
                readEventArgsWithId.SendSAEA.UserToken = e.AcceptSocket;
                Interlocked.Increment(ref this.m_numConnectedSockets);//修改连接数
                OnConnectChanged(m_numConnectedSockets);

                Socket soc = (Socket)readEventArgsWithId.ReceiveSAEA.UserToken;
                Boolean willRaiseEvent = soc.ReceiveAsync(readEventArgsWithId.ReceiveSAEA);//侦听到客户端连接开始接受数据(重要)
                if (!willRaiseEvent)
                {
                    this.ProcessReceive(readEventArgsWithId.ReceiveSAEA);
                }
            }
            catch (Exception ex)
            {
                string uid = GetIDByIP((e.AcceptSocket.RemoteEndPoint as IPEndPoint).Address.ToString());//根据IP获取用户的UID
                Err.Pro(ex);
                Err.Pro(new Exception("UID:" + uid));
            }
            finally
            {
                this.StartAccept(e);
            }
        }

youngjunzhou的主页 youngjunzhou | 初学一级 | 园豆:22
提问于:2012-07-17 14:05
< >
分享
所有回答(1)
0

请问如何解决的。

悉路 | 园豆:206 (菜鸟二级) | 2015-06-09 10:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册