Hi, I’m currently developing a simple interactive multi-user world (for mainly chatting, and walking around exploring the world) in Java.
I’ve only 2 years of experience in Java and games programming in general, and this is my first project of its kind.
Anyway, I’ve implemented a Client/Server framework, which *technically" I can use for any generic networked application, but I plan to build my multi-user game world on top of this framework. It’s part of my computer science final project at university.
Anyway, I’ve mainly used guesswork to build the various parts of the client and server framework, i’ll list and describe them, just wondering how it compares to how things are done in a standard way. Any help and feedback would be much appreciated thanks…
The issues are:
- Packet protocols. I use bufferedwriter/reader to communicate between sockets, and the client and server sends packets to each other. The packet is structured as follows
a string consisting of:
function code (byte), data1 (byte), data2 (string)
and there are single characters in between each of these elements to delimit them.
The function code describes the high-level function that is being sent to client/server, the data1 describes any data needed for that function, and data2 is a string of variable length, if required.
For example, the client sends a global chat command to the server as follows:
*7&0"Hello this is a test message@
- & " and @ are just delimiters.
7 is the function code for GLOBAL_CHAT_SEND
0 means nothing, no data needs to be sent here
and data 2 contains the message to send (Hello this is a test message)
Obviously I have code to handle things properly if the user sends a message containing a delimiter character.
Anyway my question is, should i bother using start and finish delimiters? Is byte-byte-String a efficient protocol, and comprehensive enough for sending any type of command? What’s the standard way that most programs send protocols?
- Multi-threaded servers I did not use NIO, nor did i use java.concurrent. I created the multi-threaded server from scratch… is there anything I should look out for? I think the threads work well. I use wait() and notifyAll() alot in the objects that need it, so the threads dont consume much CPU (they dont constantly poll the object for changes)
What is the standard way of modelling a multithreaded server? Is my way OK?
- The client and server framework can be distributed to anyone, and they could use it to develop their own networked application. To use it, for example a server, they instantiate my main server class, and implement 2 interfaces. The first is ReceiveListener interface, which is where all the received packets from the server go
The 2nd is ServerMessageListener, which listens for server messages, any error messages or whatever is useful for the application.
I’m worried that these 2 points are just two small. My point is, all received packets from the server are redirected down into one single abstract method receivePacket(). Information about the connection is passed down with the packet also, so the application can reply. Do you think this one point of packet flow here is not enough?
- Finally Broadcasting
In the server framework, to broadcast, the server simply iterates all connections and sends the packet to them. I’m worried that with a high number of connections, the last one will take significantly longer to receive the packet than the first one. Is this standard practice?
I had the idea of storing the packet to be broadcasted in an object which all the connection threads observe, however, how can I determine when all clients have sent the message and the next broadcasted message can be added onto the queue?
Thanks for reading this long post.
I hope you can help a poor student