Jump to content
  • Advertisement
Sign in to follow this  
Demx

Sending Objects with sockets

This topic is 2779 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

Hello,

ive been trying to create a server and client that can communicate by sending objects in Java.

I started by sending a predefined Date object and that works:

Server:

public void run() {
try{
oos.writeObject(new Date());
oos.flush();
oos.close();
client.close();
}catch(Exception e) {}
}

Client:

date = (Date) ois.readObject();
System.out.print("The date is: " + date);


The problem is that i can't get it to work proparly with my objects.

Example class:

public class Player implements Serializable {

private int hp;

public Player(){
hp=0;
}

public void sethp(int hp){
this.hp=hp;
}

public int gethp(){
return hp;
}
}

Server:

Player p = new Player();
p.sethp(15);
oos.writeObject(p);
oos.flush();

Client:

Player p = new Player();
p = (Player) ois.readObject();
hp = p.gethp();
System.out.println("player hp: " + hp );

When i run the programs the client should print the hp value of the object but it gives the following exception : server.Player

Thx for any help :)

Share this post


Link to post
Share on other sites
Advertisement
I don't partitcularly know any Java. But, is there an equivalent WSAGetLastError() call that you can use?

If there is, this will give you an error code that will help point you in the direction of what is wrong.

Share this post


Link to post
Share on other sites
Your description of the exception is incomplete. Your code with the Date object suggests you may be swallowing exceptions - don't do this! Either catch and deal with the exception, declare the method as throwing said exception type or wrap the exception in an unchecked exception (e.g. RuntimeException).

Can you post a minimal example that demonstrates the problem?

As something to think about, you almost certainly don't want to use Java's object serialisation for fast paced action games. Even in slower games, you have to be careful to avoid serialising a complex object graph.

Share this post


Link to post
Share on other sites

When i run the programs the client should print the hp value of the object but it gives the following exception : server.Player



Your problem lies in your use of Java Object Serialization.

If you want to do this, then first, make sure your objects can be serialized to and from files on disk in a single program. This will be much easier to debug than a networked version.

Second, make sure that the size of each block of data is sent before the actual data, so that you know how much data to receive before you attempt to de-serialize.

Third, beware that Java Object Serialization generally generates very large payload packets, and is unsuitable for most real-time game networking needs.

Share this post


Link to post
Share on other sites

Second, make sure that the size of each block of data is sent before the actual data, so that you know how much data to receive before you attempt to de-serialize.
[/quote]
I'm not sure that this is easy to do, as I don't know of an AP call I to get the size of a serialised object before the fact. You could serialise it in memory I suppose, but that seems a little complex.

Besides, I believe Java handles this case itself. If you try to deserialise an object from an ObjectInputStream, it will block until it can construct an entire object. The serialised form either explicitly or implicitly contains enough information so that the Object deserialisation process knows how much data it needs to read.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!