what is the library should I use ? what are the functions that help me?
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.
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.