Example #1
0
 public bool Del(AsyncSocket arg)
 {
     bool ret = false;
     if (arg != null)
     {
         lock (this.pool)
         {
             pool.Remove(arg);
             ret = true;
         }
     }
     else
     {
         ret = false;
     }
     return ret;
 }
Example #2
0
 public bool Add(AsyncSocket arg)
 {
     bool ret = false;
     lock (this.pool)
     {
         if (arg != null && this.pool.Count < capacity)
         {
             pool.Add(arg);
             ret = true;
         }
         else
         {
             ret = false;
         }
     }
     return ret;
 }
        /// <summary>
        /// 处理收到连接请求
        /// </summary>
        private void AcceptConnectRequest(IAsyncResult ar)
        {
            //有连接时会执行回调函数
            //server执行Close时,停止异步连接,也会执行此回调函数
            if (!isStart)  //isStart==false
            {
                return;
            }

            try
            {
                Socket listener = (Socket)ar.AsyncState;

                Socket client = listener.EndAccept(ar);


                AsyncSocket socket = new AsyncSocket();
                socket.Attach(client);

                if (clientPool.Add(socket))
                {
                    socket.DisConnectRequestEvent += new SocketEventHandler(AsyncSocket_DisConnectRequestEvent);

                    Interlocked.Increment(ref this.numConnectedSockets);
                    string outStr = String.Format("客户 {0} 连入, 共有 {1} 个连接。", client.RemoteEndPoint.ToString(), this.numConnectedSockets);
                    mainForm.Invoke(mainForm.setlistboxcallback, outStr);
                }
                else
                {
                    //达到最大连接数
                    socket.Disconnect();
                    mainForm.Invoke(mainForm.setlistboxcallback, "达到最大连接数,监听结束");
                    return;
                }
                listener.BeginAccept(new AsyncCallback(AcceptConnectRequest), listener);//死循环等待处理连接请求
            }
            catch (SocketException sex)
            {
                mainForm.Invoke(mainForm.setlistboxcallback, "监听结束" + sex.Message);
            }
            catch (Exception ex)
            {
                mainForm.Invoke(mainForm.setlistboxcallback, ex.Message);
            }
        }
        public bool Del(AsyncSocket arg)
        {
            bool ret = false;

            if (arg != null)
            {
                lock (this.pool)
                {
                    pool.Remove(arg);
                    ret = true;
                }
            }
            else
            {
                ret = false;
            }
            return(ret);
        }
        public bool Add(AsyncSocket arg)
        {
            bool ret = false;

            lock (this.pool)
            {
                if (arg != null && this.pool.Count < capacity)
                {
                    pool.Add(arg);
                    ret = true;
                }
                else
                {
                    ret = false;
                }
            }
            return(ret);
        }
Example #6
0
        private void AsyncSocket_DisConnectRequestEvent(AsyncSocket e)
        {
            this.clientPool.Del(e);

            Interlocked.Decrement(ref this.numConnectedSockets);
            string outStr = String.Format("�ͻ� {0} �Ͽ�, ���� {1} �����ӡ�", e.socket.RemoteEndPoint.ToString(), this.numConnectedSockets);
            mainForm.Invoke(mainForm.setlistboxcallback, outStr);
        }
Example #7
0
        /// <summary>
        /// �����յ���������
        /// </summary>
        private void AcceptConnectRequest(IAsyncResult ar)
        {
            //������ʱ��ִ�лص�����
            //serverִ��Closeʱ��ֹͣ�첽���ӣ�Ҳ��ִ�д˻ص�����
            if (!isStart)  //isStart==false
            {
                return;
            }

            try
            {
                Socket listener = (Socket)ar.AsyncState;

                Socket client = listener.EndAccept(ar);

                AsyncSocket socket = new AsyncSocket();
                socket.Attach(client);

                if (clientPool.Add(socket))
                {
                    socket.DisConnectRequestEvent += new SocketEventHandler(AsyncSocket_DisConnectRequestEvent);

                    Interlocked.Increment(ref this.numConnectedSockets);
                    string outStr = String.Format("�ͻ� {0} ����, ���� {1} �����ӡ�", client.RemoteEndPoint.ToString(), this.numConnectedSockets);
                    mainForm.Invoke(mainForm.setlistboxcallback, outStr);
                }
                else
                {
                    //�ﵽ���������
                    socket.Disconnect();
                    mainForm.Invoke(mainForm.setlistboxcallback, "�ﵽ���������,��������");
                    return;
                }
                listener.BeginAccept(new AsyncCallback(AcceptConnectRequest), listener);//��ѭ���ȴ�������������
            }
            catch (SocketException sex)
            {
                mainForm.Invoke(mainForm.setlistboxcallback, "��������" + sex.Message);
            }
            catch (Exception ex)
            {
                mainForm.Invoke(mainForm.setlistboxcallback, ex.Message);
            }
        }