关于Socket的一部接收方法AcceptAsync
自己做了个聊天工具,想在服务端用这种的AcceptAsync异步方法去接收连接对象,
发现改成这样后,虽然是异步接收,【SocketAsyncEventArgs】对象触发不了自己的_Completed事件,
如下代码:
private void Listen()
{
//实例化通信对象
ipEndPiont =
new IPEndPoint(IPAddress.Parse(txtIp.Text), int.Parse(txtPort.Text));
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//SocketAysncEventArgs objects to allocate
argsPool = new SocketArgsPool(10);
bufferPool = new BufferPool(1024,1024);
try
{
//设定监听
ServerSocket.Bind(ipEndPiont);
ServerSocket.Listen(10);
//分配【SocketAsyncEventArgs】对象
SocketEventArg = argsPool.CheckOut();
//设定缓冲区大小
bufferPool.CheckOut(SocketEventArg);
SocketEventArg.Completed +=
new EventHandler<SocketAsyncEventArgs>(OnConnectRequest_Completed);
bool IsHadle = ServerSocket.AcceptAsync(SocketEventArg);
}
catch
{
bufferPool.CheckIn(SocketEventArg);
argsPool.CheckIn(SocketEventArg);
throw new SocketException((int)SocketEventArg.SocketError);
}
}
大牛们帮忙,通信这块初学者,在线等待