After many headaches, I have decided it is in my best interest to convert my client/server game to UDP from TCP. I am not interested in starting any TCP vs UDP discussion in this thread.
Currently, the setup is as follows (simplified)
Client
- Attempts connection to server. If success…
- Creates a thread to listen for messages from server.
- Processes client input, sends messages from server, interprets server response.
Server
- Waits for connection attempts. If a connection happens …
- Create a thread to listen for messages from client.
- Processes server input, sends messages to client, interprets client messages.
Pretty much the same on both sides except that there will be multiple threads generated by the server, one for each client.
I am having a fairly difficult time finding useful UDP examples to reference. I was using http://systembash.com/content/a-simple-java-udp-server-and-udp-client/ as a reference, but one main concern comes to mind.
Right now, when the server gets an incoming connection from a client, I create a new Socket for that client and pass it to the listen thread. With this UDP example, it seems to just accept any traffic over a given port … so all of my traffic would go towards a single DatagramSocket.receive running on the main server. I guess I can get the IP address from the message in order to determine what player it belongs to but it really makes the whole thread part useless if all the messages will come to the same exact place. Am I correct in thinking this way?
Possible UDP setup?:
Client
- Attempt to communicate with server. If successful…
- Creates a thread to listen for messages from server.
- Processes client input, sends messages from server, interprets server response.
Server
- Create Main listen thread
- Main Listen thread gets ALL messages from ALL clients. As soon as a message comes in, it determines which client it belongs to and hands the message off to a client-specific thread (or creates one if this is the client’s first time connecting)
- Client Listen thread processes the message and passes the response back to the client.
- Main Listen thread gets ALL messages from ALL clients. As soon as a message comes in, it determines which client it belongs to and hands the message off to a client-specific thread (or creates one if this is the client’s first time connecting)
- Processes server input, sends messages to client, interprets client messages.