Exemplo n.º 1
0
        public void StartServer(string ip, int port, int maxClient)
        {
            semaphore    = new Semaphore(maxClient, maxClient);
            pool         = new ClientPeerPool(maxClient);
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            for (int i = 0; i < maxClient; i++)
            {
                ClientPeer client = new ClientPeer();
                client.ReceiveCompleted += OnRecieve;
                pool.Push(client);
            }

            serverSocket.Bind(new IPEndPoint(IPAddress.Parse(ip), port));
            serverSocket.Listen(maxClient);

            app.Init();//初始化app
            Console.WriteLine("服务器启动成功");
            StartAccept(null);
        }
Exemplo n.º 2
0
 private void StartReceive(ClientPeer client)
 {
     client.clientSocket.BeginReceive(client.receiveBuffer, 0, ClientPeer.bufferSize, SocketFlags.None, ReceiveCallback, client);
 }
Exemplo n.º 3
0
 /// <summary>
 /// 客户端接收数据后的回调
 /// </summary>
 /// <param name="client"></param>
 /// <param name="msg"></param>
 private void OnRecieve(ClientPeer client, NetMsg msg)
 {
     app.ReceiveMessage(client, msg);
 }
Exemplo n.º 4
0
 /// <summary>
 /// 归还一个客户端对象
 /// </summary>
 /// <param name="client"></param>
 public void Push(ClientPeer client)
 {
     pool.Enqueue(client);
 }