Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 40: Object transport via datagram packets

Transport Java objects over the network with datagram packets

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
One of the attractive features of Java 1.1 is its inclusion of the classes 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.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (3)
Login
Forgot your account info?

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

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources