Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an object over the Internet

I define a class, and then I instate an object of that class type. I want to send this object to another Java application running on a different computer transparently. What is the best technology to accomplish this?

like image 455
joemoe Avatar asked Dec 19 '09 00:12

joemoe


People also ask

How do you send an object over a network?

Java provides (binary) object serialization using the ObjectOutputStream (and ObjectInputStream). You can just writeObject() into the stream and readObject() on the other end. All you need to do for this to work is implement the Serializable interface.

What is object output stream?

An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream.


2 Answers

You'll want to start by looking into serialization with the Java Serializable interface. Sun has a good article on it called Discover the secrets of the Java Serialization API.

Refer to the Java Sockets tutorial for information on actually transferring the serialized object over the network.

like image 188
Bill the Lizard Avatar answered Sep 22 '22 17:09

Bill the Lizard


you can create object streams using the java API and send any serializable object. but you'll have to mind that these go unencrypted through the network:

on the sender's side:

CustomObject objectToSend=new CustomObject();
Socket s = new Socket("yourhostname", 1234);
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
out.writeObject(objectToSend);
out.flush();

and on the receiving end:

ServerSocket server = new ServerSocket(1234);
Socket s = server.accept();
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
CustomObject objectReceived = (CustomObject) in.readObject();
like image 30
fasseg Avatar answered Sep 22 '22 17:09

fasseg