Ejemplo n.º 1
0
        private AsyncServer()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            int           parallelNum = 0, wait4ClientTimeOut = 0;

            //默认可以同时接收500个连接请求。收到请求后如果数量超过1个,则弹出列表供用户选择
            if (!(int.TryParse(config.AppSettings.Settings["ParallelNum"].Value, out parallelNum)))
            {
                parallelNum = 500;
            }

            m_NumConnections           = parallelNum;
            m_ReceiveBufferSize        = ProtocolConst.ReceiveBufferSize;
            m_AsyncSocketUserTokenPool = new AsyncSocketUserTokenPool(parallelNum);
            m_AsyncSocketUserTokenList = new AsyncSocketUserTokenList();
            m_MaxNumberAcceptedClients = new Semaphore(parallelNum, parallelNum);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 初始化服务器
        /// </summary>
        /// <param name="_port">端口</param>
        /// <param name="_parallelNum">最大连接数</param>
        /// <param name="_socketTimeOutMS">超时,单位毫秒</param>
        public SocketServer(int _port, int _parallelNum, int _socketTimeOutMS)
        {
            port            = _port;
            parallelNum     = _parallelNum;
            socketTimeOutMS = _socketTimeOutMS;

            receiveBufferSize        = ProtocolConst.ReceiveBufferSize;
            maxNumberAcceptedClients = new Semaphore(parallelNum, parallelNum);
            asyncSocketUserTokenList = new AsyncSocketUserTokenList();
            asyncSocketUserTokenPool = new AsyncSocketUserTokenPool(parallelNum);

            AsyncSocketUserToken userToken;

            for (int i = 0; i < parallelNum; i++) //按照连接数建立读写对象
            {
                userToken = new AsyncSocketUserToken();
                userToken.ReceiveEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
                userToken.SendEventArgs.Completed    += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
                asyncSocketUserTokenPool.Push(userToken);
            }
        }