2

I'm looking to send & receive Java objects to & from another Java application. This application may be on another machine. My need for this is that I am running multiple Java game servers and need them all to talk to the one central server. I've tried looking about for some pointers on how to do this but found nothing.

I'd imagine you start off with with creating a new thread for each game server and then opening a Socket to the server.

3 Answers 3

3

You need:

ObjectOutputStream and ObjectInputStream

To use these methods implement an interface called serializable in you class

here you are going to serialize employee class i.e you can share objects of employee class

Demo:

public class employee implemetns serializable
{
}

public class SerializeDemo 
{
   public static void main(String [] args)
   {
      Employee e = new Employee();
      e.name = "Reyan Ali";
      e.address = "Phokka Kuan, Ambehta Peer";
      e.SSN = 11122333;
      e.number = 101;
      try
      {
         FileOutputStream fileOut =
         new FileOutputStream("/tmp/employee.ser");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(e);
         out.close();
         fileOut.close();
         System.out.printf("Serialized data is saved in /tmp/employee.ser");
      }catch(IOException i)
      {
          i.printStackTrace();
      }
   }
}


public class DeserializeDemo
{
   public static void main(String [] args)
   {
      Employee e = null;
      try
      {
         FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         e = (Employee) in.readObject();
         in.close();
         fileIn.close();
      }catch(IOException i)
      {
         i.printStackTrace();
         return;
      }catch(ClassNotFoundException c)
      {
         System.out.println("Employee class not found");
         c.printStackTrace();
         return;
      }
      System.out.println("Deserialized Employee...");
      System.out.println("Name: " + e.name);
      System.out.println("Address: " + e.address);
      System.out.println("SSN: " + e.SSN);
      System.out.println("Number: " + e.number);
    }
}

Now you can simply send this file to other servers or clients using sockets!.

hope this helps

Sign up to request clarification or add additional context in comments.

3 Comments

Seems somebody is playing jokes with us. Both our answers are OK. Not sure who is downvoting around.
seriously man! Those who downvoted us please provide more accurate answers ..we would really like to read your answers!
Thanks for your answer, I think I'll be using this method but instead with wrapper classes which will only contain the data needed to send.
1

You should look at
http://docs.oracle.com/javase/7/docs/api/java/io/DataOutputStream.html
http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html
http://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html
http://docs.oracle.com/javase/7/docs/api/java/io/ObjectInputStream.html

You can use these for sending/receiving primitive values or objects between the apps. Yes, you can use Thread (for handing 2 or more clients on the server at the same time), Socket (as a communication channel between the apps), and ServerSocket (to listen for incoming connections from clients on the server side).

2 Comments

Both sides need to listen for incoming connections as objects will be sent back and forth, does this mean each game server needs to have its own port? If so that's going to be a pain
If you run not more than one game server on one machine, you can have the same port used (e.g. 1234 on each machine). But if you run two game servers on one machine, then the two game servers need to have different ports.
1

Unlike the other answers, I'm not going to suggest ObjectStreams (I'm not the downvoter though). Sure, they would be one way to send Java Objects directly, but you don't really want to do that. ObjectStreams also behave a bit specially compared to other streams.

Map down what information you need to transfer between the servers/clients, and then design your protocol around that. You could use a binary protocol, or even send something like JSON, but sending full objects is most likely not the best idea.

It seems you don't have a lot of experience with networking, so you should look up some examples from other projects.

2 Comments

I agree with that. In real world you want to use some protocol that can tunnel through proxies and firewalls. That leaves pretty much only HTTP and HTTPS as the two options. After that you will be left with long polling / comet protocols. Text data like JSON is better than binary data for such protocols.
Thanks, however I think that's overly complex for my needs. I think I'll use ObjectStreams but instead send Packet instances instead of full classes. Thanks for taking time to post your answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.