Sending an item over the Internet

I define a class and then set up an object of this type of class. I want to send this object to another Java application running on another computer transparently. What is the best technology for this?

+7
java serialization networking
source share
5 answers

you can create streams of objects using the java API and send any serializable object. but you should keep in mind that they are not encrypted over the network:

on the sender 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 side:

 ServerSocket server = new ServerSocket(1234); Socket s = server.accept(); ObjectInputStream in = new ObjectInputStream(s.getInputStream()); CustomObject objectReceived = (CustomObject) in.readObject(); 
+7
source share

You want to start with serialization using the Java Serializable interface. Sun has a good article on it. Discover the secrets of the Java Serialization API .

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

+11
source share

There are many ways to do this. Here are some things to look out for, and you can choose the one that works best for your application.

  • J2ee
  • Rmi
  • Serialization of objects pushing bits through Socket
  • Webservices

To a large extent, any communication infrastructure will allow you to push objects over the network in one way or another. You just need to look through them and see what works for your application. A quick google should find even more methods.

+6
source share

The standard A (de facto) for implementing this will be to use a web service , for example using JAX-WS , which is associated with Java 6. See this tutorial for the first Java example (i.e. using annotations). It is quite simple and easy.

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

+2
source share

Java provides (binary) serialization of objects using ObjectOutputStream (and ObjectInputStream). You can simply writeObject () to the stream and readObject () at the other end. All you have to do for this is to implement the Serializable interface.

But instead of doing it manually, you may be interested in taking it one level and using the method's remote method call. With RMI, you can call methods on objects that live in another JVM, and all serialization and networking takes place under the hood.

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

+1
source share

All Articles