Hey everyone. I am in the process of designing a Java-based 2D multiplayer game engine type thing (let me know if that was too technical :)), and I thought it best to seek out the advice of those more experienced in such things. I’d like to know your opinions on the efficiency, inefficiency, neatness or sloppiness of my current network system. Any feedback is greatly appreciated!
First of all, I am using UDP sockets (because I hear they are superior for making realtime/high-speed gameplay over a network).
When the server is started, a new DatagramSocket is created with a user-specified port. A new thread is created solely for receiving packets on this new DatagramSocket (we’ll call it the listen socket). When a packet is received, the server creates a new thread with a new DatagramSocket specifically for communication with this client (this socket is bound to whatever port the system decides). It then sends a verification message to the client on the new socket so that the client knows the server is there, and to give the client the port of the new socket. It sends this message 8 times (for handshake-type packets, I use redundancy to virtually guarantee arrival despite using UDP). The server stores a list of all client threads for later use.
If, however, the server receives a message on the listen socket and there is already a client thread with that address, it ignores the packet. This is virtually guaranteed to happen, since we are using redundancy on the client side as well.
To join the server, we need the IP address and port of the server’s listen socket (these could be obtained through a matchmaking system, or just using direct IP or LAN or something like that). The client sends a join request to the server 8 times (again, redundancy, the server will ignore all but one of these). We then wait for the verification packet from the server (timeout after ten seconds or so if we don’t get one). Once we get one, we start a new thread that tries to receive data from the server (using a DatagramSocket, of course).
The Network Cycle
On the server, each client thread tries to receive data from a specific client. Any data that is received is stored in an ArrayList of packets (packets are stored as byte arrays). On the client the same thing happens, except there is only one thread instead of many, since the client need only communicate with the server. During every update cycle, we get the list of received packets and properly process each one, then we clear the list. We update the game, then we send any data that we need to send.
My question is, is this a good design? Is there any way I could make it better? If you have any questions, tips, comments, or anything just let me know. Thanks in advance for your advice!