【发布时间】:2015-01-15 05:18:43
【问题描述】:
TcpListener tcpServer = new TcpListener(8080);
tcpServer.Start();
tcpServer.BeginAcceptTcpClient(new AsyncCallback(this.messageRecived), tcpServer);
我有一些代码用于接受来自 tcp 客户端的读取,客户端以这种方式发送消息:
TcpClient client = new TcpClient("192.168.0.2", 8080)
string Str = "it's work";
byte[] Buffer = Encoding.ASCII.GetBytes(Str);
client.GetStream().Write(Buffer, 0, Buffer.Length);
问题出在 messageRecived 方法中:
public void messageRecived(IAsyncResult result)
{
byte[] data = new byte[20000];
string outData;
TcpListener server = (TcpListener)result.AsyncState;
server.AcceptTcpClient().GetStream().Read(data, 0, server.Server.ReceiveBufferSize);
outData = Encoding.ASCII.GetString(data);
addLog(outData);
}
当我向服务器发送消息时,接收到的方法运行到这一行:
server.AcceptTcpClient().GetStream().Read(data, 0, server.Server.ReceiveBufferSize);
在下一次迭代中,它从下一行开始到方法的结尾。有什么问题?
【问题讨论】:
标签: c# .net asynchronous tcplistener