ObjectInputStream and ObjectOutputStream. With this new API (the method writeObject(Object o) in class ObjectOutputStream and the method object readObject() in class ObjectInputStream), one can get a snapshot of running objects at any time, no matter how complex the object graph. Because such a snapshot
is provided through the ObjectOutputStream class, which is a descendent of the OutputStream class, one can easily wrap this stream with any other output stream, thereby implementing any desirable functionality (such
as a FileOutputStream).The presence of these new classes in Java 1.1 makes it possible to transmit a running object over the network. To do this,
the object, along with those objects that have been referenced, must be serializable -- that is, able to be converted into
a byte stream. Fortunately, in Java 1.1, most built-in classes are serializable. Some classes, however, are not (class Object is a prime example). Not to worry. If your class is derived from a non-serializable class, serialization can be achieved
by using method defaultWriteObject() in the ObjectOutputStream class, and can then be deserialized by method defaultReadObject() in class ObjectInputStream.
Once serialized, the object is ready to transmitted over a network. The following example demonstrates how to make an object serializable and how to send it through a stream socket:
//Object Output
import java.net.*;
import java.io.*;
//The sample class to be sent: Factory
class Factory implements Serializable
{ private void writeObject(ObjectOutputStream out) throws IOException
{ out.defaultWriteObject(); }
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException
{ in.defaultReadObject(); }
}
public class ShowObjOutput
{ public static void main(String[] arg)
{ try
{ ObjectOutputStream os;
Socket sock = new Socket("panda.cs.uno.edu", 6000);
//panda is the host name
Factory fa = new Factory();
os = new ObjectOutputStream( new
BufferedOutputStream(sock.getOutputStream()));
os.writeObject(fa);
}
catch (IOException ex)
{}
}
}
This next example shows you how an object is received by ObjectInputStream from a stream socket:
//Object Input
import java.net.*;
import java.io.*;
public class ShowObjInput
{ public static void main(String[] arg)
{ try
{ ObjectInputStream is;
ServerSocket servSock = new ServerSocket(6000);
Sock sock;
sock = servSock.accept();
is = new ObjectInputStream( new
BufferedInputStream(sock.getInputStream()));
Factory o = (Factory)is.readObject();
} catch (IOException ex)
{}
}
}
Besides tightly coupled sockets, Java also supports connectionless datagram communication with the DatagramSocket class. Can we achieve object input/output with datagram communication? Accomplishing this is not as straightforward as with
stream sockets. The problem is that the DatagramSocket is not attached to any stream; rather, a DatagramSocket takes a byte array as parameter for sending and receiving.
12 Years and no comment that this example never workedBy Anonymous on November 17, 2009, 12:35 amAfter 12 years , no one has actually tried to compile this and comment that it does not work. Where is dSock defined? In the first example, it will not compile...
Reply | Read entire comment
Very helpfulBy Anonymous on October 31, 2009, 4:03 pmThanks this is a very clear article. Good job
Reply | Read entire comment
ThanksBy Anonymous on June 12, 2009, 1:39 amCongratulations Shengxi Zhou. It's a very well written article. It was simple to follow and up to the point. Warm Regards, ghd
Reply | Read entire comment
Thank you very much! I am going to implement the same...By Anonymous on March 23, 2009, 8:59 pmThank you very much! I am going to implement the same. I am sure this must work.
Reply | Read entire comment
thanks heaps for ur helpBy Anonymous on October 5, 2008, 11:55 amthat was avery comprehensive description of how serialisation works in sockets. At many other places they give no idea about serialising using datagram sockets. Thanks...
Reply | Read entire comment
View all comments