I wrote the following Tcp Server applictaion application. The problem is that it does not execute individual clients in parallel. That is, if one client is connected, the server does not accept connections to other clients. Please help me fix the code:
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)
{
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)
{
await ProcessData(dataReceived);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
if (client.Connected)
client.Close();
}
}
source
share