One of the attractive features of Java 1.1 is the addition of ObjectInputStream and ObjectOutputStream. With this API (writeObject (Object o) method in the ObjectOutputStream class and object readObject () in the ObjectInputStream class), you can get a snapshot of the running Object at any time, no matter how complicated the object graph is. Because such snapshots are provided through the ObjectOutputStream class (subclass of the OutputStream class), you can easily wrap them in other output streams to implement any required functions (such as FileOutputStream ).
These new classes provided in Java 1.1 make it possible to transmit running objects online. To this end, the object and those referenced objects must be serializable-that is, they can be converted to byte streams. Fortunately, in Java 1.1, Most built-in classes are serializable. However, some classes are not serializable (the Object class is a typical example ). But don't worry. If your class inherits from a class that is not serializable, you can use the defaultWriteObject () method in the ObjectOutputStream class to serialize it. Then, you can use the defaultReadObject () method in the ObjectInputStream class to unserialize it.
Once serialized, the object can be transmitted online. The following example shows how to generate a serializable object and send it through a stream socket:
// Object output
Import java.net .*;
Import java. io .*;
// Example of the 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)
{}
}
}
The next example illustrates how ObjectInputStream receives objects 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)
{}
}
}
In addition to tightly coupled sockets, Java also provides the initramsocket class to support connectionless datagram communication. Can we use datagram communication to complete object input/output? Isn't it as easy to complete this function as using stream sockets? The problem is that mongoramsocket is not connected to any stream. To perform sending and receiving operations, mongoramsocket uses a byte array as the parameter.
As you can imagine, in order to construct a data packet, the object must be converted into a byte array. If an object involves a complex object graph, this conversion may be extremely difficult. Many previous articles have discussed how to implement Object serialization-packaging (serialization) Java objects into word throttling and unpacking byte streams as Java objects. However, because the object graph may be complex, you may need to write a lot of code to convert a regular object graph into a byte array.
So how can we avoid writing complicated packaging code? The following provides a method to transmit objects using a datagram packet without writing packaging code.
Follow the seven steps below to implement this data stream, which can transmit any type of object, myObject.
Step 1. Preparation: implement the Serializable interface to make your object (for example, myObject) Serializable.
Step 2. Create a ByteArrayOutputStream object, for example, baoStream.
Step 3. Use baoStream to construct an ObjectOutputStream object, for example, ooStream.
Step 4. Write the object myObject to baoStream by calling the writeObject () method of ooStream.
Step 5. Use the toByteArray () method of baoStream to retrieve the byte array buffer from baoStream.
Step 6. Use the Array Buffer retrieved in step 5 to construct the DatagramPacket, for example, dPacket.
Step 7. Call the send () method of mongoramsocket to send dPacket.
To receive objects, complete the steps listed above in reverse order, replace ObjectOutputStream with ObjectInputStream, and replace ByteArrayInputStream with ByteArrayOutputStream.
When using socket programming, sendTo is a standard function used in the connectionless protocol. In order to be able to transmit objects, I have rewritten this function. The following code example shows how to implement the send method in the Sender class:
Import java. io .*;
Import java.net .*;
Public class Sender
{
Public void sendTo (Object o, String hostName, int desPort)
{
Try
{
InetAddress address = InetAddress. getByName (hostName );
ByteArrayOutputStream byteStream = new
ByteArrayOutputStream (5000 );
ObjectOutputStream OS = new ObjectOutputStream (new
BufferedOutputStream (byteStream ));
OS. flush ();
OS. writeObject (o );
OS. flush ();
// Retrieve byte Arrays
Byte [] sendBuf = byteStream. toByteArray ();
DatagramPacket packet = new DatagramPacket (
SendBuf, sendBuf. length, address, desPort );
Int byteCount = packet. getLength ();
DSock. send (packet );
OS. close ();
}
Catch (UnknownHostException e)
{
System. err. println ("Exception:" + e );
E. printStackTrace ();
}
Catch (IOException e)
{E. printStackTrace ();}
}
}
The following code list shows how to implement the receive method in the Explorer class. The recvObjFrom method is used by the receiver to receive objects. You should include this method in your code to receive runtime objects.
Import java. io .*;
Import java.net .*;
Public class extends er
{
Public Object recvObjFrom ()
{
Try
{
Byte [] recvBuf = new byte [1, 5000];
DatagramPacket packet = new DatagramPacket (recvBuf,