Jump to content
  • Advertisement
Sign in to follow this  
jolyqr

Networking game with Java

This topic is 4266 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Basically, I'm trying to create a networking game by using Java. I would like to use sockets for allowing clients and the server to communicate. In the only examples I have found, the client and the server were sending string messages. I would like to know if there is a way of sending an object containing for instance all game states needed... cheers

Share this post


Link to post
Share on other sites
Advertisement
Java RMI is done through serialization, which is basically converting an object to a string ...

Object Player{
health = 14;
Xcoord = 2;
Ycoord = 11;
mood = onery;

serialize(){
return "14,2,11,*";
}

unserialize(serialstr){
arr[] = explode(serialstr);
this.health = arr[0];
this.Xcoord = arr[1];
this.Ycoord = arr[2];
this.mood = translateMood(arr[3]);
}

translateMood(newmood){
if(newmood == *){
return "onery";
}else{
return Undef;
}
}
}

java serialization says Player(14,2,11,*)

Dont knock strings. TCP packets and binary is a better way to compress than strings, but it's not as easy to work with. In the end, everything you want to send or receive has to be put in/read from linear messages, so strings are as good as any until you have bandwidth issues.

Share this post


Link to post
Share on other sites
You can send objects over sockets, without resorting to RMI, via ObjectOutputStream and ObjectInputStream in java.io. That means all of the classes you intend to send must be serializable.

Defining a packet protocol would be a better option, but if you are working directly with sockets in java.net you'll have to implement some things that are taken care of for you if you use java.nio instead. Through java.nio, you don't write data to IO streams or work directly with Socket objects. Instead, you write data to ByteBuffers and manipulate SocketChannels. The ByteBuffer API makes it very easy to work with packets. Aside from these benefits, you can implement a much more efficient server using java.nio than you can with java.net.

Share this post


Link to post
Share on other sites
Quote:
Original post by Aldacron
You can send objects over sockets, without resorting to RMI, via ObjectOutputStream and ObjectInputStream in java.io. That means all of the classes you intend to send must be serializable.

Defining a packet protocol would be a better option, but if you are working directly with sockets in java.net you'll have to implement some things that are taken care of for you if you use java.nio instead. Through java.nio, you don't write data to IO streams or work directly with Socket objects. Instead, you write data to ByteBuffers and manipulate SocketChannels. The ByteBuffer API makes it very easy to work with packets. Aside from these benefits, you can implement a much more efficient server using java.nio than you can with java.net.


I'm just starting in Java. Could you give me examples of a packet protocols and the use of java.nio?

Share this post


Link to post
Share on other sites
Quote:
Original post by Jack9
Java RMI is done through serialization, which is basically converting an object to a string ...

Object Player{
health = 14;
Xcoord = 2;
Ycoord = 11;
mood = onery;

serialize(){
return "14,2,11,*";
}

unserialize(serialstr){
arr[] = explode(serialstr);
this.health = arr[0];
this.Xcoord = arr[1];
this.Ycoord = arr[2];
this.mood = translateMood(arr[3]);
}

translateMood(newmood){
if(newmood == *){
return "onery";
}else{
return Undef;
}
}
}

java serialization says Player(14,2,11,*)

Dont knock strings. TCP packets and binary is a better way to compress than strings, but it's not as easy to work with. In the end, everything you want to send or receive has to be put in/read from linear messages, so strings are as good as any until you have bandwidth issues.


I didn't really understand your advice? Do you think I should use RMI instead of Sockets and Sending strings instead of trying to send Object?

Share this post


Link to post
Share on other sites
My client and server are ready. I have created a tcp socket such as the one explain on the following page : http://java.sun.com/docs/books/tutorial/networking/sockets/index.html

Each time a client connects to the server, the server creates a thread for it.
I don't know how to make clients communicate. As clients send messages to different server threads, how could they communicate each others ?

Share this post


Link to post
Share on other sites
Quote:
Original post by jolyqrI'm just starting in Java. Could you give me examples of a packet protocols and the use of java.nio?


A "packet" is just a term for a set of related data that you send over the internet. A "packet protocol" defines what type of packets your application supports.

Any packet protocol should include three basic components: a packet id, the size of the packet data, and the data itself. Normally, the information about a packet (id and size) is called the "header" and the data is called the "body" or the "payload". So you might implement a packet structure like this:

16 bytes : packet size
16 bytes : packet id
packet-size bytes : data

This means that whenever you have data to send between the client and server, your networking code should send it in the above format. As an example, say you have a game object with x and y coordinates, and you want to send the coordinates from the server to the client. Assuming that both values are of type int, your packet size will be 64 bytes, since an int in Java is 32 bytes and you are sending two of them. In your protocol, let's say you've assigned the number 10 to represent game object position updates. Using a ByteBuffer, you would do something like the following:


buffer.putShort(64); // the size of the data segment of the packet
buffer.putShort(10); // the id of the game object position update packet
buffer.putInt(go.getX()); // the x coordinate of the game object
buffer.putInt(go.getY()); // the y coordinate of the game object




You would then, using java.nio, send that data to the client via a SocketChannel. When the client reads the packet and sees the value 10 is the packet id, then it will know that it has a game object update packet and can extract the appropriate data.

Just so you're clear, packet protocols have nothing to do with Java or any language. They simply define a standard way for you to send data across the wire so that a client and a server know how to communicate with each other.

I explained the above because it's something that can be explained in a brief post. The java.nio package, and how to use it, can not be. It's not necessarily a difficult topic, but lengthy. You need to understand networking basics to understand java.nio. It's not something you learn overnight.

Developing an application using java.net and strings, as you are, should help you with getting to grips with the basics. But I would like to suggest that you develop a simple text-based client-server app as a research project before continuing with your game. You might find it beneficial.

Share this post


Link to post
Share on other sites
Quote:
Original post by jolyqrEach time a client connects to the server, the server creates a thread for it.
I don't know how to make clients communicate. As clients send messages to different server threads, how could they communicate each others ?


The clients don't communicate with each other in a client-server app. That's what the server is for. The clients communicate directly with the server. The server then processes any game state calculations and sends updates out to every client.

If you can get it, this book is very useful for learning both java.net and how to work with multiple threads. It will go a long way to helping you understand the topic. With the knowledge you can pick up from that book, you'll be ready to tackle java.nio next.

Share this post


Link to post
Share on other sites
Thanks, everything is clearer now.

I'm just trying to code a simple tic tac toe. The book you recommanded me seems interesting.

Unfortunately, this time I'm out of money, I won't be able to buy it for now. Nevertheless, if you know some good links, don't hesitate to give them to me...

Share this post


Link to post
Share on other sites
Quote:
Original post by Aldacron
Quote:
Original post by jolyqrEach time a client connects to the server, the server creates a thread for it.
I don't know how to make clients communicate. As clients send messages to different server threads, how could they communicate each others ?


The clients don't communicate with each other in a client-server app. That's what the server is for. The clients communicate directly with the server. The server then processes any game state calculations and sends updates out to every client.


I have some basics in networking. I know that clients don't communicate directly each others.

Let me explain you my problem by using a simple example.

A game can be played only when there are two clients.

When client 1 connects to the server, a server thread is created.
When client 3 connects to the server, a server thread is created.

Two different thread servers are created. How could the server see information located in both threads?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!