Java multiplayer game - network concepts

For a school project, we need to create a multi-player game in Java (it must be a client / server) that can be played over the Internet (we program it in school, so this is not homework). The game is turn-based, but there should be a chat, which, of course, in real time. However, none of us have any programming experience on the net, and the more I read about it, the more questions I have.

My first thought was to use the socket API to implement the multi-user part. The server expects new data from clients. However, there are several types of data to receive, such as chat messages, traffic, etc. In addition, after the connection with the server is done, it is necessary to send some initial data (for example, the name of the player). The server should be able to see what message it received, but how? I was thinking of creating a Message class with a type string field. But in my server code, I will get the code as follows:

 if (message.type.equals("message")) { // code to execute for chat messages } else if (message.type.equals("movement")) { // code to execute for movement } else if () { // ... } else { // ... } // Please ignore syntax errors :P 

When there are many different types of data to send (and there will be), this does not seem to be the most efficient way. In addition, this would mean that both the server and the client must have this Message-class / interface (duplicate code).

What about other games? For example, player 1 moves his character to a position that defeats another character. Player 1's client calculates this defeat and applies the correct actions. But what needs to be sent to the server? Only a new player position or defeat? With the first option, this means that all other clients must perform the calculations. Could this not cause any problems? Since I don't have prior network programming experience, I got a little confused about how to do all this.

I also read in another thread on Stackoverflow that RMI might be a better option. After reading some information about this, I understand what RMI is, but I still cannot figure out if this is a good option for this project or not. Any tips for this?

As you can see, I'm a little confused about how to start from the network part of this project. I was looking for some programming books (for Java, of course), but none of them focus on the network part. I also searched for Java network books, but they seem to be focused on technology, not good code practices.

If someone knows a good book or has any tips for proper disintegration, we will be very grateful.

thanks

+7
source share
2 answers

You are on the right track, but there is something that can be clarified.

If you use sockets, you find out that you need to define a protocol - a common language for conveying moves and game status. Sockets allow you to send any data in almost any format. It looks like you are thinking of serializing the Message class to send this type, this is one of the things. If you use RMI (which has its own protocol), you will act as if you were calling Java methods, but essentially doing something similar, serializing the data and passing it through the socket.

There is nothing unclear how to exchange code between the client and the server - in fact, most services do this in one form or another. The client and server can use a common library to determine which classes of messages to send. RMI uses method stubs to define an interface. All kinds of web services determine how methods are called. The general idea is to expose the interface, not the implementation.

As for your code, it might be cleaner to have a different Message subclass for each type of message, and you could add additional parameters for each message. Then you can have a MessageProcessor class:

 class MessageProcessor{ void process(Move1Message m) {...} void process(Move2Message m) {...} .... } 

Regarding what to send, the general principle should be that the client is responsible for sending his move to the server, all that he does is a bonus, because the server needs to check the legality of the move. The server should always be the defining state of the game in order to avoid cheating and erroneous client implementations.

If you are not interested in learning how to implement your own protocol or use the Java socket library, it will be easier to use RMI. You can also use SOAP, REST or any other protocol, but I would not think too much about which one to use at the moment. I have no suggestions other than RMI documentation , although I think this book had a lot of code examples for networking.

+7
source

when switching with sockets, each client will have its own connection on which the server stream will wait (do not forget to clear the stream on the client side or you will always wait)

each line will be a separate message and to distinguish between message types, you can use the "header" at the beginning of each message (a specific 3-character sequence at the beginning), say msg , mov , lgn and use the trie-like selection with the help of switches to quickly decide which one did you get


when using RMI, you can have a server manager object (exported and registered in the registry) on which the client can request a “connection” object, which will be the same as the connection in the socket implementation, you can have a way for every thing that you want to do, although callbacks must be done in some other way (with sockets you have a connection ready for it)

0
source

All Articles