Sending an object over the Internet - java

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?

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.

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();

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.

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).

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.

Related

How can I send an instance over a socket connection?

I'm attempting to write two Java programs.
One to simulate a server, and one to simulate a client.
How could I go about sending an instance of a Response class over a socket?
The Response class represents status codes of the server connection. e.g. 404 Not Found etc
I'm not allowed to use Serialisation unfortunately.
Any advice would be greatly appreciated.
At some level Serialization must occur in order to send an object across a connection. I can only assume your comment about being not allowed to use serialization refers to not being able to use Serializable instead of a blanket prohibition of serialization(which makes no sense). A very simple method to accomplish this would be the use of a external serialization library such as gson. Gson serializes an object into a JSON string that you can transmit over your socket and then using the same library deserialize it back into an object on the other side. You can of course use any of your preferred serialization libraries with your favorite format eg. XML, json, YAML,...
You wouldn't be sending an instance of the Response class itself. When sending things over a network, client and server machines understand bytes. Your application can understand more than bytes, it can understand specific representations. For example, your server might send a JSON representation of your Response class like:
{
"response" : {
"code":404
}
}
Then your client must be able to understand what this sequence of bytes means. That's basically what a protocol is: how two machines can communicate.
Regardless of what language the server or clients are written in, the Response is an Entity. In Java you might use a Class to represent it, in C++ you might use a struct. However, both would need to know that when you are communicating with an external application.system, they would have to put it in a format that everyone understands, be it json, xml, or any other.
As for sending this through sockets, Oracle has a nice tutorial here. You get the OutputStream
from the socket and start writing your representation.

Sending a complex object from Java client to C server via Socket

I want to send some complex objects from a Java client to C server via a TCP Socket.
How can I do that ?
Fundamentally the question is, "How to serialize/deserialize objects in a
language agnostic manner?" Specifically Java and C in your case. Since you'll
be sending this data over a network, it is also important to take care of network order/endianness issues.
I assume you have access to both the the client and the server. This means you
get to choose how to serialize the data. (If not, the answer is simple. Write
to the specs of what the other is expecting)
Personally, I would use Protocol Buffers.
There are Java bindings
and C bindings.
If you don't like Protocol Buffers, there are other options like:
JSON (already mentioned)
YAML
Apache Thrift
XDR
roll your own
...
Write the fields of the Java objects to a string (perhaps JSON), send them via TCP, and have the C program read the string and use it to initialize new C variables on the other end.
This question is pretty old, but just in case some one is still looking for a good solution, you can try out the protocol buffers implementation, as mentioned in the previous answer by #Adam Liss: (developers.google.com/protocol-buffers/)
In short, you define any complex message type as in your protocol implementation, and the tool generates C++/Java/Python code which can serialize and deserialize it.
For the same purpose using C code, a research project at the Technische Universität München (TUM) Germany have created a code generator in standard C, that can be used with embedded-C projects. This is fully compatible(with limitations due to C structs) with Google's protobuf implementation. This works better than the C Bindings because it does not need any library to be linked with.
I had issues in getting the C Bindings to work on the embedded systems I was working with, because it needs to be linked with the support library.
This saved my (painful) day with my embedded project - passing complex network data(request-responses) between an embedded system and Android app(Java)/Desktop app(C++/Qt).

Sending object via socket, but different language client, how to make the object serialize?

I have a Java Server that sending the java serializable object to my client, and receive java serializable object for execution. If my client is also java written, which is nice, that allow me to do communication within any problems.
But now, I would like to extend my programme to not only java client, the client may be written in C, objective C, python or php. So, I would like to do something to "convent" to client request to a java object, and send back to Server. The convent process, I can use the JSON to receive, and construct a Java object to the Server, but I also need a layer that convert back the Java object to JSON to the client.
My Question is except make a JSON-Java Translation layer, is there any other ways to do so? Also, we can afford to change some code in server side, but we must use Java as our primary language for that. Any suggestions? Thanks.
I use Netty API for designing my protocol and it is quite quick to do so if you can understand a NIO-like Byte and Buffer API.
It is design to work with a concept of Encoder and Decoder that could fit your need, there are a lot of default implementation of Encoder and Decoder for zipping, using ssl...
The problem you have seems to looks like this one:
JBoss Netty with JSON
I don't know JSON very well but most of the time is could also be quick and easy to design your own protocol.
Do you need a generic Serialization process for any kind of Object or do you simply need to serialize some String and primitive types (Integer, Short, Float..etc)?
In the case of simple objects it is easy and a lot faster to do the wrapper by yourself.
If objects are quite simple, and I would guess this is the case, your need it to design your own "protocol" specification meaning how to turn each Object into a sequence of primitive types, String and arrays. Than it should be quite easy to write both the Encoder and the Decoder in each language.
Good luck
There are other libraries designed for this, like protocol buffers and thrift.
http://thrift.apache.org/
http://code.google.com/p/protobuf/

How to transfer objects over network using java

what is the library should I use ?
what are the functions that help me?
The easiest way is probably using serialization. Therefore, your object classes must implement serializable, so do have all of the members (primitves and most of the standard java classes already do this). This allows the mapping between object instances and byte streams at runtime.
You also need a protocol for transer. You can have a look at RMI, if you don't want to deal with streaming you byte streams over the wire, though this is not that difficult.
Using RMI however allows you build more powerful distributed java applications later.
ObjectOutputStream/ObjectInputStream.
The whole logic is approximately as follows. Adjust by the demands of your app.
Send:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(objectToSend);
oos.close();
byte[] bytes = baos.toByteArray();
socket.write(bytes);
Receive:
ObjectInputStream ois = new ObjectInputStream(socketInputStream);
MyObject mo = (MyObject)ois.readObject();
It Depends:
If the other endpoint is in Java then Java serialization might be the fastest way to implement. But note supporting Java Serialization is not a trivial task and can be a chore to maintain over time. Just google "Java serialization gotchas" for some examples.
If the other endpoint is not in Java or future maintainability and compatibility is a goal, then I would recommend a more general reusable encoding.
For this I would look into Google Protocol Buffers or Apache Thrift (I can post only 1 hyperlink).
Of course, there is always the option of using XML to encode your objects. :)
Personally, in our projects we have been using Google Protocol Buffers and in my opinion can't be beat for ease of use, maintainability, and most important in our case, compatibility between Protocol Buffer versions.
If you are in a multi-platform environment, you can use CORBA. Although it is a little complicated because you may need to control both endpoints and implement the Interface Definition Language (IDL) bindings.
A good alternative is using JSON. You just need to map the JSON structure into your Java objects.

Object(Output|Input)Stream binary protocol

I was wondering if anyone had some resources that describe the binary protocol used by ObjectOutputStream. I realize of course that objects themselves can specify what their data by implementing the Externalizable interface, so I guess I'm looking more toward the structure of the object graph - the metadata if you will.
I am writing a C program that has to talk to a legacy Java program. I have no way to change either of these requirements so find myself reverse engineering the ObjectOutputStream protocol. (There is a server that uses HTTP for transport and returns Object*Stream as the HTTP response.)
However, I feel like someone else out there has to have done this work before. Can you point to any resources to speed up my work?
http://java.sun.com/javase/6/docs/technotes/guides/serialization/index.html
and from there
http://java.sun.com/javase/6/docs/platform/serialization/spec/protocol.html

Categories