BrandonPotter / SimpleTCP

Straightforward .NET library to handle the repetitive tasks of spinning up and working with TCP sockets (client and server).
Apache License 2.0
363 stars 108 forks source link

Calling TcpClient.Close() prevents subsequent DataReceived events for other clients #45

Open chxmbley opened 6 years ago

chxmbley commented 6 years ago

There are several points in my application where the server needs to disconnect a client. For some reason, calling TcpClient.Close() in an event handler prevents the DataReceived event from being invoked when other clients send data.

Below is a sample that demonstrates this issue:

var server = new SimpleTcpServer();

server.DataReceived += (s, a) =>
{
    Console.WriteLine("Data");
    a.TcpClient.Close();
};

server.ClientConnected += (s, a) =>
{
    Console.WriteLine("Connected");
};

server.Start(8088);

Console.ReadLine();
server.Stop();

Using the code above and an application like PacketSender to test, I expect that a connection would be closed each time data is sent to the server. Instead, the first client to connect to the server is disconnected after data is sent and subsequent connections never raise the DataReceived event.