コード例 #1
0
        const int iClientMaxCount = 20;//最大客户端数量

        static void Main(string[] args)
        {
            LogManager.Init();//清除Log文件夹
            AsyncIOCPServer iocp = new AsyncIOCPServer(IP, portNo, iClientMaxCount);

            Console.ReadLine();
        }
コード例 #2
0
        /// <summary>
        /// 侦听客户端
        /// </summary>
        public AsyncIOCPServer(string IP, int portNo, int maxClient)
        {
            instance = this;
            try
            {
                IPAddress ipAddress = IPAddress.Parse(IP);
                s_Server     = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                hostEndPoint = new IPEndPoint(ipAddress, portNo);
                s_Server.Bind(hostEndPoint);
                s_Server.Listen(maxClient);

                userTokenPool = new AsyncUserTokenPool(maxClient);
                for (int i = 0; i < maxClient; i++) //填充SocketAsyncEventArgs池
                {
                    AsyncUserToken userToken = new AsyncUserToken(bufferSize);

                    userToken.SAEA_Receive.Completed += new EventHandler <SocketAsyncEventArgs>(OnIOCompleted);
                    userToken.SAEA_Receive.UserToken  = userToken;
                    userTokenPool.Push(userToken);
                }

                Thread tCheckClientHeartbeat = new Thread(CheckClientHeartbeat);
                tCheckClientHeartbeat.IsBackground = true;
                tCheckClientHeartbeat.Start();

                StartAccept(null);
                Log4Debug("初始化服务器。");
            }
            catch (Exception error)
            {
                Log4Debug(error.Message);
            }
        }