Esempio n. 1
0
        /// <summary>
        /// 开启服务器
        /// </summary>
        public void StartServer(string ip, int port, int maxClient)
        {
            try {
                clientPeerPool = new ClientPeerPool(maxClient); // 客户端连接池实例
                // 初始化 Semaphore 类的新实例,并指定初始入口数和最大并发入口数
                semaphore = new Semaphore(maxClient, maxClient);
                // 创建 Socket 对象
                serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                // 填满客户端对象连接池,需要的时候再去取
                for (int i = 0; i < maxClient; i++)
                {
                    ClientPeer temp = new ClientPeer();
                    temp.receiveCompleted       = ReceiveProcessCompleted; // 消息处理完成后的委托,注册函数
                    temp.ReceiveArgs.Completed += ReceiveArgs_Completed;   // 注册事件
                    clientPeerPool.Enqueue(temp);
                }

                // 绑定
                serverSocket.Bind(new IPEndPoint(IPAddress.Parse(ip), port));
                // 监听该接口上的请求
                serverSocket.Listen(maxClient);
                Console.WriteLine("服务器启动成功");

                // 连接
                StartAccept(null);
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// 开启服务器
 /// </summary>
 public void StartServer(string ip, int port, int maxClient)
 {
     try
     {
         clientPeerPool = new ClientPeerPool(maxClient);
         semaphore      = new Semaphore(maxClient, maxClient);
         serverSocket   = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         //填满客服端对象连接池
         for (int i = 0; i < maxClient; i++)
         {
             ClientPeer temp = new ClientPeer();
             temp.receiveCompleted      += ReceiveProcessCompleted;
             temp.ReceiveArgs.Completed += ReceiveArgs_Copmleted;
             clientPeerPool.Enqueue(temp);
         }
         //绑定到进程
         serverSocket.Bind(new IPEndPoint(IPAddress.Parse(ip), port));
         //最大监听数
         serverSocket.Listen(maxClient);
         Console.WriteLine("服务器启动成功");
         StartAccept(null);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
Esempio n. 3
0
 /// <summary>
 /// 客户端断开连接
 /// </summary>
 /// <param name="client"></param>
 /// <param name="reason"></param>
 private void Disconnect(ClientPeer client, string reason)
 {
     try {
         if (client == null)
         {
             throw new Exception("客户端为空,无法断开连接");
         }
         Console.WriteLine(client.ClientSocket.RemoteEndPoint + "客户端断开连接,原因" + reason);
         application.Disconnect(client); // 应用层断开连接
         client.Disconnect();            // 让客户端处理断开连接
         clientPeerPool.Enqueue(client); // 回收
         semaphore.Release();            // 退出信号量并返回前一个计数
     }
     catch (Exception e) {
         Console.WriteLine(e.Message);
     }
 }
Esempio n. 4
0
        /// <summary>
        /// 客户端断开连接
        /// </summary>
        /// <param name="client"></param>
        /// <param name="reason"></param>
        private void Disconnect(ClientPeer client, string reason)
        {
            try
            {
                if (client == null)
                {
                    throw new Exception("客服端为空,无法连接");
                }
                application.Disconnect(client);
                Console.WriteLine(client.clientSockert.RemoteEndPoint + "断开连接原因:" + reason);
                client.Disconnect();

                clientPeerPool.Enqueue(client);
                semaphore.Release();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }