natli
natli

Reputation: 3822

Knowing which UDP client is which

Is it possible to identify an earlier UDP client without keeping the socket open? I want to link an integer ID to each unique client, but I don't want to keep any additional threads open.

//Receive (Server)

private static Int32 port = 11000;
private static UdpClient udpClient = new UdpClient(port);

public static void receive_threaded()
{
    Thread t = new Thread(() =>
    {
        while (true)
        {
            IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, port);
            byte[] content = udpClient.Receive(ref remoteIPEndPoint);

            if (content.Length > 0)
            {
                string message = Encoding.UTF8.GetString(content);
                if (action_message_receive != null) action_message_receive(String.Format("Recv({0}): {1}", remoteIPEndPoint.Port, message));
                parseMessage(message);
            }
        }
    });
    t.Start();
}

//Send (Client)

private static void send_message(string ip, string message)
{
    byte[] packetData = System.Text.UTF8Encoding.UTF8.GetBytes(message);

    int port = 11000;

    IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ip), port);
    Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    client.SendTo(packetData, ep);

    if (action_message_send != null) action_message_send("Send: " + message);
}

A client can request a (temporary) userID from the server, and the server will add it to it's database and notify the client when it's done doing that. However, I can't have the client send it's own userID when it's making requests because any memory altering application would mean a 'hacker' could gain access to other user's stuff.

Since the socket doesn't stay open, the IPEndPoint.Port changes every time the client sends something to the server, so I can't keep track of it with that. I could get it done by creating a username/pass on a userID request and having those sent on every single request involving the userID thereafter, but that would be silly.

So is there any way to do this without keeping a thread open for each client? I'm probably doing something really weird here because UDP is supposed to be a one way street, but I'm here to learn so I just had to ask.

Upvotes: 0

Views: 2380

Answers (1)

Gung Foo
Gung Foo

Reputation: 13558

You will need to introduce some sort of unique identifier chosen by the server and sent to the client for it to "behave and send it back for identification purposes". A random long integer should suffice.

UDP has neither connections nor identification/authentication mechanisms. If you want those, use TCP (but those can be bruteforced also...)

Upvotes: 2

Related Questions