Sending an object over the Internet
Asked Answered
F

5

8

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?

Fogdog answered 19/12, 2009 at 0:28 Comment(2)
You mean like--satellite, the Internet or AM/FM-radio? Or if not, may we see the class? Give us code dude! \o/ Anything! As long as it's still moving, a little..Henequen
BTW, you asked for the best technology to accomplish your question not sample code how to do it. That is why you got 3 answers for technologies to use. If you want sample code then ask for sample code and you would been provided with sample code from multiple people on how to do it using different technologies or using the same but you would have more to choose from.Laurettelauri
L
11

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();
Lavaliere answered 19/12, 2009 at 0:48 Comment(0)
W
12

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.

Wraparound answered 19/12, 2009 at 0:30 Comment(0)
L
11

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();
Lavaliere answered 19/12, 2009 at 0:48 Comment(0)
L
6

There are a lot of ways to do this. Here are some things to look into and you can pick the one that would work best for your application.

  • J2EE
  • RMI
  • Object Serialization pushing the bits over a Socket
  • Webservices

Pretty much any communication framework will allow you to push objects over a network in one way or another. You just need to review them and see which works for your application. A quick google should find even more methods.

Laurettelauri answered 19/12, 2009 at 0:33 Comment(0)
V
2

A (de facto) standard to implement this would be to use a web service, for example using JAX-WS which is bundled in Java 6. See this tutorial for a java-first sample (i.e. using annotations). This is pretty straight forward and easy.

There are other approaches such as Serialization over a Socket, RMI, EJBs but, when working over the Internet, web services are a kind of natural choice as they rely on existing standards (SOAP, HTTP) and deal easily with firewalls (which might be a real issue for all other solutions).

Veloz answered 19/12, 2009 at 1:1 Comment(0)
I
2

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.

But rather than doing that manually, you may be interested in taken it one level up and using Remote Method Invocation. With RMI you can call methods on objects that live in another JVM, and all the serialization and networking happens under the hood.

And for the sake of completeness, there is also XML bean serialization, if you cannot use the binary format. That XML format is very generic (read: verbose and ugly), but there are some popular libraries (like XStream) that create alternative XML serializations.

Inaugural answered 2/9, 2015 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.