1.对TcpListener的异步Accept方法:
AsyncCallback CallBack = new AsyncCallback(curr.ReturnAcceptCallBack);
Server.BeginAcceptTcpClient(CallBack, Server);
public void ReturnAcceptCallBack(IAsyncResult ar)
{
try
{
Console.WriteLine("Star AcceptCallBack");
TcpListener Server = (TcpListener)ar.AsyncState;
TcpClient client = Server.EndAcceptTcpClient(ar);
Console.WriteLine(" AcceptCallBack Finished");
NetworkStream River = client.GetStream();
//直接进行接收
AsyncCallback ReadCallBack = new AsyncCallback(BeginReadProcess);
River.BeginRead(RecvBytes, 0, RecvBytes.Length, ReadCallBack, River);
}
catch(Exception ex)
{
wr.WriteLine("Exception Info:" + ex.Message);
wr.Flush();
throw ex;
}
}
2.对NetworkStream的异步处理
3. 还是要保证,连接的正确关闭,在关闭的连接上不再对该连接上写数据.
4.关于tcplistener tcpclient
TcpClient.Close Method
The Close method marks the instance as disposed and closes the TCP connection. Calling this method will close the Socket and also close the associated NetworkStream that is used to send and receive data if one was created.
NetworkStream Constructor (Socket, FileAccess, Boolean)
5.begin end 分别做什么事情
C# 异步TCP通信:TcpListener与TcpClient实践
本文介绍了使用C#实现TCP通信的异步非阻塞方式,通过TcpListener的BeginAcceptTcpClient方法启动接受客户端连接,并在回调函数中处理连接。同时,对NetworkStream进行了异步读取操作。强调了正确关闭连接的重要性,以及TcpClient.Close方法和NetworkStream构造函数的作用。
2万+

被折叠的 条评论
为什么被折叠?



