private void InitOperatePool() { argsPool = new SocketAsyncPool(100); for (int i = 0; i < 100; i++) { var acceptArgs = new SocketAsyncEventArgs(); acceptArgs.Completed += (ss, ee) => { manual.Set(); this.displayBox.Invoke(new MethodInvoker(delegate() { this.displayBox.Text += Environment.NewLine + "建立了一个连接。"; })); var connectedSocket = ee.AcceptSocket; var receiveArgs = new SocketAsyncEventArgs(); byte[] bufferTem = new byte[100]; receiveArgs.SetBuffer(bufferTem, 0, 100); receiveArgs.Completed += (receiveS, receiveE) => { this.displayBox.Invoke(new MethodInvoker(delegate() { this.displayBox.Text += Environment.NewLine + Encoding.GetEncoding("GB2312").GetString(receiveE.Buffer); })); connectedSocket.ReceiveAsync(receiveArgs); }; connectedSocket.ReceiveAsync(receiveArgs); }; argsPool.PushSocketAsyncEventArgs(acceptArgs); } }
private void InitOperatePool() { argsPool = new SocketAsyncPool(100); for(int i = 0; i < 100; i++) { var acceptArgs = new SocketAsyncEventArgs(); acceptArgs.Completed += (ss, ee) => { manual.Set(); this.displayBox.Invoke(new MethodInvoker(delegate() { this.displayBox.Text += Environment.NewLine + "建立了一个连接。"; })); var connectedSocket = ee.AcceptSocket; var receiveArgs = new SocketAsyncEventArgs(); byte[] bufferTem = new byte[100]; receiveArgs.SetBuffer(bufferTem, 0, 100); receiveArgs.Completed += (receiveS, receiveE) => { this.displayBox.Invoke(new MethodInvoker(delegate() { this.displayBox.Text += Environment.NewLine + Encoding.GetEncoding("GB2312").GetString(receiveE.Buffer); })); connectedSocket.ReceiveAsync(receiveArgs); }; connectedSocket.ReceiveAsync(receiveArgs); }; argsPool.PushSocketAsyncEventArgs(acceptArgs); } }