/// <summary> /// 用来开启服务器 /// </summary> /// <param name="port">端口号</param> ///<param name="maxCount">最大连接数量</param> public void Start(int port, int maxCount) { try { serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); acceptSemaphore = new Semaphore(maxCount, maxCount); //直接new出最大数量的连接对象 clientPeerPool = new ClientPeerPool(maxCount); ClientPeer tmpClientPeer = null; for (int i = 0; i < maxCount; i++) { tmpClientPeer = new ClientPeer(); tmpClientPeer.ReceiveArgs.Completed += receive_Completed; tmpClientPeer.receiveCompleted = receiveCompleted; tmpClientPeer.sendDisconnect = Disconnect; clientPeerPool.Enqueue(tmpClientPeer); } serverSocket.Bind(new IPEndPoint(IPAddress.Any, port)); serverSocket.Listen(10); Console.WriteLine("服务器启动..."); startAccept(null); } catch (Exception e) { Console.WriteLine(e.Message); throw; } }
/// <summary> /// 开启服务器 /// </summary> /// <param name="port">端口号</param> /// <param name="maxCount">最大连接数量</param> public void Start(ushort port, ushort maxCount) { try { serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sema = new Semaphore(maxCount, maxCount); clientPool = new ClientPeerPool(maxCount); ClientPeer clientPeer = null; for (int i = 0; i < maxCount; i++) { clientPeer = new ClientPeer(); clientPeer.ReceiveArgs.Completed += Receive_Compelet; clientPeer.rc = ReceiveCompeleted; clientPeer.sd = Disconnect; clientPool.EnqueueClient(clientPeer); } serverSocket.Bind(new IPEndPoint(IPAddress.Any, port)); serverSocket.Listen(maxCount); Console.WriteLine("服务器启动成功"); // 服务器启动成功之后, 就开始监听客户端的接入(StartAccept()) StartAccept(null); } catch (Exception e) { Console.WriteLine(e.Message); } }
private Semaphore acceptSemaphore; //位于system.Threanding 下。限制可同时访问某一资源池的线程数。 /// <summary> /// 开启服务器 1,bind 2,listen 3.accept /// </summary> /// <param name="port"></param> /// <param name="maxCount"></param> public void Start(int port, int maxCount) { try { serverPeer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); acceptSemaphore = new Semaphore(maxCount, maxCount); //在连接之前就调用客户端线程池 初始化 clientPeerPool = new ClientPeerPool(maxCount); ClientPeer clientPeer = null; for (int i = 0; i < maxCount; i++) { clientPeer = new ClientPeer(); clientPeer.receiveArgs.Completed += Receive_Complete; clientPeer.receiveCompleted = ReceiveCompleted; clientPeer.sendDisConnected = DisConnectd; clientPeerPool.Enqueue(clientPeer); } serverPeer.Bind(new IPEndPoint(IPAddress.Any, port)); serverPeer.Listen(10); Console.WriteLine("服务器启动成功..."); StartAccept(null); } catch (Exception e) { Console.WriteLine(e.Message); throw; } }
/// <summary> /// 用来开启服务器 /// </summary> /// <param name="port"></param> /// <param name="maxCount"></param> public void Start(int port, int maxCount) { try { acceptSemaphore = new Semaphore(maxCount, maxCount); serverSocket.Bind(new IPEndPoint(IPAddress.Any, port)); serverSocket.Listen(maxCount); clientPeerPool = new ClientPeerPool(maxCount); ClientPeer tmpClientPeer = null; for (int i = 0; i < maxCount; i++) { tmpClientPeer = new ClientPeer(); tmpClientPeer.ReceiveArgs.Completed += Receive_Completed; tmpClientPeer.snedDisconnect = DisConnect; tmpClientPeer.receiveCompleted = ReceiveCompleted; clientPeerPool.EnQueue(tmpClientPeer); } StartAccept(null); Console.WriteLine("服务器启动成功。。。"); } catch (Exception e) { Console.WriteLine(e.Message); } }
/// <summary> /// 开启服务器 /// </summary> /// <param name="port">端口</param> /// <param name="maxCount">最大连接数量</param> public void Start(int port, int maxCount) { try { //修复bug ,设置最大的线程数, 参数:现在可用的线程数量, 最大可用的线程数量 acceptSemaphore = new Semaphore(maxCount, maxCount); //创建线程池 clientPool = new ClientPeerPool(maxCount); ClientPeer tempClient = null; for (int i = 0; i < maxCount; i++) { tempClient = new ClientPeer(); tempClient.ReceiveArgs.Completed += processReceiveCompeleted; //用委托来为client 提供函数 tempClient.receiveCompeleted += receiveCompleted; //发送的时候发生异常了,就直接断开连接,简单粗暴 tempClient.sendDisConnect += DisConnect; clientPool.Enqueue(tempClient); // } //任何人都可以接通的 serverSocket.Bind(new IPEndPoint(IPAddress.Any, port)); //把 10 个线程至于监听状态,不用每一次访问都得重新创建 serverSocket.Listen(10); //服务器启动 startAccept(null); } catch (Exception e) { //报错 Console.WriteLine(e.Message); throw; } }
/// <summary> /// 开启服务器 /// </summary> /// <param name="port">端口号</param> /// <param name="maxCount">最大连接数量</param> public void Start(int port, int maxCount) { try { serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); acceptSemaphore = new Semaphore(maxCount, maxCount); clientPeerPool = new ClientPeerPool(maxCount); ClientPeer tempClientPeer = null; for (int i = 0; i < maxCount; i++) { tempClientPeer = new ClientPeer(); clientPeerPool.Enqueue(tempClientPeer); } serverSocket.Bind(new IPEndPoint(IPAddress.Any, port)); serverSocket.Listen(maxCount); Console.WriteLine("服务器启动"); startAccept(null); } catch (Exception e) { Console.WriteLine(e.Message); } }