我编写了以下Tcp服务器应用程序。问题是它不能同时执行单个客户端。也就是说,如果一个客户端连接,服务器将不接受其他客户端的连接。请帮我修复代码:
void Run()
{
tcpListener.Start();
while (true)
{
Console.WriteLine("Waiting for a connection...");
try
{
var client = await tcpListener.AcceptTcpClientAsync();
await Accept(client);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
private async Task Accept(TcpClient client)
{
//get client information
string clientEndPoint = GetClientIPAddress(client);
Console.WriteLine("Client connected at " + clientEndPoint);
log.Info("Client connected at " + clientEndPoint);
await Task.Yield ();
try
{
using (client)
using (NetworkStream stream = client.GetStream ())
{
byte[] dataReceived = new byte [50];
while (await stream.ReadAsync(dataReceived, 0, dataReceived.Length) != 0) //read input stream
{
//process data here
await ProcessData(dataReceived);
}
}
} //end try
catch (Exception ex)
{
Console.WriteLine(ex.Message);
if (client.Connected)
client.Close();
}
} //end Accept(TcpClient client)