ServerSocket and Socket for Serializable object : Socket « Network Protocol « Java






ServerSocket and Socket for Serializable object

      


import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Iterator;
import java.util.Vector;

class ComplexCompany implements Serializable {
  private String name;

  private ComplexEmployee president;

  private Vector departments;

  public ComplexCompany(String name) {
    this.name = name;
    departments = new Vector();
  }

  public String getName() {
    return this.name;
  }

  public void addDepartment(ComplexDepartment dept) {
    departments.addElement(dept);
  }

  public ComplexEmployee getPresident() {
    return this.president;
  }

  public void addPresident(ComplexEmployee e) {
    this.president = e;
  }

  public Iterator getDepartmentIterator() {
    return departments.iterator();
  }

  public void printCompanyObject() {
    System.out.println("The company name is " + getName());
    System.out.println("The company president is " + getPresident().getName());
    System.out.println(" ");

    Iterator i = getDepartmentIterator();
    while (i.hasNext()) {
      ComplexDepartment d = (ComplexDepartment) i.next();
      System.out.println("   The department name is " + d.getName());
      System.out.println("   The department manager is " + d.getManager().getName());
      System.out.println(" ");
    }
  }

}

class ComplexDepartment implements Serializable {
  private String name;

  private ComplexEmployee manager;

  public ComplexDepartment(String name) {
    this.name = name;
  }

  public String getName() {
    return this.name;
  }

  public ComplexEmployee getManager() {
    return this.manager;
  }

  public void addManager(ComplexEmployee e) {
    manager = e;
  }
}

class ComplexEmployee implements Serializable {
  private String name;

  private int salary;

  /** Creates a new instance of ComplexEmployee */
  public ComplexEmployee(String name, int salary) {
    this.name = name;
    this.salary = salary;
  }

  public String getName() {
    return name;
  }

  public int getSalary() {
    return this.salary;
  }
}

public class ComplexSocketServer {

  public static void main(String args[]) throws Exception {
    ServerSocket servSocket;
    Socket fromClientSocket;
    int cTosPortNumber = 1777;
    String str;
    ComplexCompany comp;

    servSocket = new ServerSocket(cTosPortNumber);
    System.out.println("Waiting for a connection on " + cTosPortNumber);

    fromClientSocket = servSocket.accept();

    ObjectOutputStream oos = new ObjectOutputStream(fromClientSocket.getOutputStream());

    ObjectInputStream ois = new ObjectInputStream(fromClientSocket.getInputStream());

    while ((comp = (ComplexCompany) ois.readObject()) != null) {
      comp.printCompanyObject();

      oos.writeObject("bye bye");
      break;
    }
    oos.close();

    fromClientSocket.close();
  }
}





public class ComplexSocketClient {

  public static void main(String args[]) throws Exception {
    Socket socket1;
    int portNumber = 1777;
    String str = "";

    socket1 = new Socket(InetAddress.getLocalHost(), portNumber);

    ObjectInputStream ois = new ObjectInputStream(socket1.getInputStream());

    ObjectOutputStream oos = new ObjectOutputStream(socket1.getOutputStream());

    ComplexCompany comp = new ComplexCompany("A");
    ComplexEmployee emp0 = new ComplexEmployee("B", 1000);
    comp.addPresident(emp0);

    ComplexDepartment sales = new ComplexDepartment("C");
    ComplexEmployee emp1 = new ComplexEmployee("D", 1200);
    sales.addManager(emp1);
    comp.addDepartment(sales);

    ComplexDepartment accounting = new ComplexDepartment("E");
    ComplexEmployee emp2 = new ComplexEmployee("F", 1230);
    accounting.addManager(emp2);
    comp.addDepartment(accounting);

    ComplexDepartment maintenance = new ComplexDepartment("Maintenance");
    ComplexEmployee emp3 = new ComplexEmployee("Greg Hladlick", 1020);
    maintenance.addManager(emp3);
    comp.addDepartment(maintenance);

    oos.writeObject(comp);

    while ((str = (String) ois.readObject()) != null) {
      System.out.println(str);
      oos.writeObject("bye");

      if (str.equals("bye"))
        break;
    }

    ois.close();
    oos.close();
    socket1.close();
  }

}

   
    
    
    
    
    
  








Related examples in the same category

1.Create a socket without a timeout
2.Create a socket with a timeout
3.Demonstrate Sockets.
4.Socket connection and concurrent package
5.XML based message
6.ObjectInputStream and ObjectOutputStream from Socket
7.String based communication between Socket
8.Get email with Socket
9.Create PrintWriter from BufferedWriter, OutputStreamWriter and Socket
10.Read from server
11.Use Socket to read and write stream
12.Connects to a server at a specified host and port. It reads text from the console and sends it to the server
13.A simple network client that establishes a network connection to a specified port on a specified host, send an optional message across the connection
14.Reading Text from a Socket
15.Writing Text to a Socket
16.Sending a POST Request Using a Socket
17.Get the Date from server
18.Transfer a file via Socket
19.Ping a server
20.Read and write through socket
21.Read float number from a Socket
22.Read Object from Socket
23.deleting messages from a POP3 mailbox based on message size and Subject line
24.A timeout feature on socket connections
25.Write Objects From Socket
26.Write Double Using Sockets
27.Download WWW Page
28.Redirects incoming TCP connections to other hosts/ports
29.Socket Fetcher
30.Socket Address Encoder
31.Zip socket
32.This program shows how to interrupt a socket channel.
33.This program shows how to use sockets to send plain text mail messages.
34.Makes a socket connection to the atomic clock in Boulder, Colorado, and prints the time that the server sends.