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
365 stars 108 forks source link

Fix: ClientDisconnected event not being triggered #16

Closed OceanAirdrop closed 7 years ago

OceanAirdrop commented 7 years ago

Hi Brandon,

I have a fix for the ClientDisconnected event not being triggered. Its a small fix. Essentially it moves your test for "clients that have been disconnected" from the bottom of the "connected clients" loop to the top (above the continue statement).

Next, I used the power of stack overflow to come up with a simpler helper function "IsSocketConnected" that does a couple of other checks in addition to testing the "Connected" property.

This works for me in my testing however totally understand if you don't wish to merge this.

Great project by the way. 👍

My sample code:

class Program
{
    static SimpleTcpServer server = null;
    static void Main(string[] args)
    {
        server = new SimpleTcpServer().Start(8910);

        server.ClientConnected += Server_ClientConnected;
        server.ClientDisconnected += Server_ClientDisconnected;
        server.DataReceived += Client_MessageReceived;

        Console.WriteLine("Starter Server. Press enter to stop...");
        Console.ReadLine();

        server.Stop();
    }

    static void Server_ClientConnected(object sender, TcpClient e)
    {
        Console.WriteLine("A new client is connected: {0}. Client Count: {1}", e.Client.RemoteEndPoint.ToString(), server.ConnectedClientsCount);
    }

    static void Server_ClientDisconnected(object sender, TcpClient e)
    {
        Console.WriteLine("A client is disconnected: {0}. Client Count: {1}", e.Client.RemoteEndPoint.ToString(), server.ConnectedClientsCount);
    }

    static void Client_MessageReceived(object sender, Message e)
    {
        Console.WriteLine("ClientId: {0} sent the message: {1}", e.TcpClient.Client.RemoteEndPoint.ToString(), e.MessageString);

        string reply = string.Format("Got it buddy! You sent: {0}", e.MessageString);
        Console.WriteLine("Sending Reply: {0}", reply);

        // Send Reply to Client
        e.ReplyLine(reply);
    }
}
BrandonPotter commented 7 years ago

Added you as collab if you're willing to merge and address the issue that's currently open on this. Thanks!

OceanAirdrop commented 7 years ago

Thank you Brandon. Yes, not a problem. Will review changes again then merge. Cheers.

kirkdmo commented 7 years ago

Can we get this merged and an updated Nuget?

BrandonPotter commented 7 years ago

@OceanAirdrop can you still do this?

OceanAirdrop commented 7 years ago

Hi. Yes, I can still do this. I am still on holiday and only checking email sporadically. No access to my computer but will look into this next week.

On 24 Aug 2017 18:36, "Brandon Potter" notifications@github.com wrote:

@OceanAirdrop https://github.com/oceanairdrop can you still do this?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/BrandonPotter/SimpleTCP/pull/16#issuecomment-324704639, or mute the thread https://github.com/notifications/unsubscribe-auth/ALxuEPYN1ysSPKt_GLPU2ituHVbK-sjIks5sbbSKgaJpZM4OVmS2 .