TCP Object Transmission in Java

07 Mar 2011 by

Marshaling or Serialization

Marshaling or Serialization

A special feature in UDP is multicast. In TCP, object transmission is its special feature. This feature enables programmers to create an instantiated object in the server and then marshalling before it is actually transmitted to client for execution.

The processes of object transmission are:

  • Marshalling object to be transmitted
  • Send marshalled object over the network
  • Unmarshalling object and the execute

What is marshalling?

Marshalling and Serialization can be used interchangeably. It is the process of squashing object into a standard format in order to store in a file or be transmitted over the network.

To do marshaling in Java, object must support serialization. All native Java objects are serializable except static variables or reference to local things such as File or Socket. For user-defined objects, it is necessary to implement Serializable interface.

Example of TCP Object Transmission

Example below is to show you to send MyDate object over the network.

MyDate.java

import java.io.Serializable;
import java.util.*;

/**
 * @author http://lycog.com
 */

//Implement Serializable to do marshaling
public class MyDate implements Serializable{
  private Date date_;
  private int number_;
  static int i = 0;

  MyDate() {
    date_ = new Date();
    number_ = i++;
  }

  //To be invoked at the client
  public Date getDate() {
    return date_;
  }

  //To be invoked at the client
  public int getNumber() {
    return number_;
  }
}

TCPObjectServer.java

import java.net.*;
import java.io.*;
/**
 * @author http://lycog.com
 *
 */
public class TCPObjectServer {
  public static void main(String[] args) {
    ServerSocket server_socket = null;
    Socket socket = null;
    try {
      server_socket = new ServerSocket(9999);

      while (true) {
        System.out.println("Wait for client ....");
        socket = server_socket.accept();
        System.out.println("Accepted from " + socket.getInetAddress());

        ObjectOutputStream oo =
                      new ObjectOutputStream(socket.getOutputStream());

        MyDate d = new MyDate();
        //Send object over the network
        oo.writeObject(d);
        oo.flush();
      }
    } catch (IOException ioe) {
      System.out.println(ioe);
    }
  }
}

TCPObjectClient.java

import java.net.*;
import java.io.*;
/**
 * @author http://lycog.com
 */
public class TCPObjectClient {
  public static void main(String[] args) {
    Socket socket = null;
    try {
      socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999);

      ObjectInputStream oi =
                   new ObjectInputStream(socket.getInputStream());

      //Read serialized object
      MyDate dd = (MyDate) oi.readObject();

      //Invoke methods
      System.out.println("MyDate = " + dd.getDate() +
                                " MyNumber = " + dd.getNumber());
    } catch (ClassNotFoundException cnfe) {
      System.out.println(cnfe);
    } catch (IOException ioe) {
      System.out.println(ioe);
    }
  }
}

 

4 responses so far

Leave a Reply