Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Java Tip 40: Object transport via datagram packets

Transport Java objects over the network with datagram packets

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.

1 | 2 |  Next >
Resources