Saturday, January 11, 2014

Sending and receiving Java Objects through network Java Program [Simple]

//program for server

import java.io.*;
import java.net.*;
import java.util.*;
class tcpobjwrite
{
 public static void main(String args[]) throws Exception
 {
   ServerSocket ss;
   Socket s;
   ss = new ServerSocket(8888);
   s = ss.accept();
   ObjectOutputStream oos;
   oos = new ObjectOutputStream(s.getOutputStream());
   oos.writeObject(new Date());
   oos.close();
   s.close();
   ss.close();
 }
}

------------------------------------------------------------------------
 
/*  Program for client  */
import java.io.*;
import java.net.*;
import java.util.*;
class tcpobjread
{
 public static void main(String args[]) throws Exception
 {
   Socket s;
   s = new Socket("localhost",8888);
   ObjectInputStream ois;
   ois = new ObjectInputStream(s.getInputStream());
   Date d = (Date)ois.readObject();

   System.out.println("Hello"+d);
   ois.close();

}
}
 

No comments: