user547794
user547794

Reputation: 14511

Async sockets - identifying unique connections

I have a server that accepts async socket connections from multiple clients. Right now I am assigning them a server-side unique ID dependent on their incoming IP address & port. My concern is - what if 2 clients connect from the same IP & behind an NAT. Is there a better way to determine this?

 private static int DetermineSocketToConnectionID(Socket handler)
        {
            int connectionIDForHandler = 0;

            string uniqueIPString = handler.RemoteEndPoint.ToString();

            if (Client1Buffers.UserIPAddress == uniqueIPString)
            {
                connectionIDForHandler = 1;
            }
            if (Client2Buffers.UserIPAddress == uniqueIPString)
            {
                connectionIDForHandler = 2;
            }

            return connectionIDForHandler;
        }

Upvotes: 1

Views: 580

Answers (1)

Jerico Sandhorn
Jerico Sandhorn

Reputation: 2020

You may be better off having those clients authenticate themselves with the server on their first connection and pass you something you can use to identify them other than trusting network information. This is exactly why sessions and cookies were created for logging in clients to web servers. You can do something similar by handshaking with the client and passing them back a uid, which they would then need to pass back to the server on each subsequent connect.

Upvotes: 1

Related Questions