I've been trying to get a simple client server relation using udp for about a week now, And I think it's time for me to stop trying and ask for help... I understand how TCP works and UDP too. The thing is, when I use UDP, there's no way for me to know the client is still connected, and same thing for the client. I tried using two connections, one TCP for sending important data such as login information, but I don't know how to keep a stable connection in UDP!
Here's how I work:
- The server starts a new thread for listening on port X with TCP
- The client connects to the server in TCP using the port X
- The server sends a confirmation for connection on the UDP part of the server
- The client starts sending information (location of the player) via udp and opens a thread for that client (tcp and udp).
- When the server hasn't received data for 15 seconds, it sends a "ping"
- If the server hasn't received "pong" in the next 5 seconds, it closes the connection + thread for this client.
- When a client is disconnected or sends a disconnect packet, it closes the connection + thread for this client.
The main problem with this is that I don't know how to receive data(server side) from a certain address via udp. At the moment, I can only receive data from every address...
Is there a way I could ensure I only receive data from a single remote computer (on the listening thread of that particular client)? Am I doing something wrong? Should work differently (seems it's making lots of connections and threads for a server that would have lots of clients)?
I'm working in C# with console applications at the moment, but I'll be using forms in the future, and the game is going to be in Java (C# XNA 4.0 for testing the connection first).
And most important of all, Thanks you!