Serve entire objects using ObjectOutputStream : Server : Network Protocol : Java examples (example source code) Organized by topic

Java
C++
Java Home »  Network Protocol   » [  Server  ]  Screenshots 
 



Serve entire objects using ObjectOutputStream



import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Hashtable;

public class ObjServer {
  public static void main(String args[]) throws Exception {
    ServerSocket ssock = new ServerSocket(1234);
    Hashtable hash = new Hashtable();
    hash.put("Java Source and Support""www.java2s.com");

    while (true) {
      System.out.println("Listening");
      Socket sock = ssock.accept();

      ObjectOutputStream ostream = new ObjectOutputStream(sock
          .getOutputStream());
      ostream.writeObject(hash);
      ostream.close();
      sock.close();
    }
  }
}

// A Java-specific client can read objects from Java servers.

class ObjClient {
  public static void main(String[] argsthrows Exception {
    Socket sock = new Socket(args[0]1234);
    ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
    Hashtable hash = (Hashtableois.readObject();
    System.out.println(hash);
    ois.close();
    sock.close();
  }
}

           
       
Related examples in the same category
1.  A generic framework for a flexible, multi-threaded server
2.  Server allows connections on socket 6123 Server allows connections on socket 6123
3.  This server displays messages to a single client This server displays messages to a single client
4.  The client can specify information to control the output of a server The client can specify information to control the output of a server
5.  A server can use specialized streams to deliver typed data A server can use specialized streams to deliver typed data
6.  A multithreaded server A multithreaded server
7.  Base class to build multithreaded servers easily
8.  Manage a pool of threads for clients
9.  Client estimates the speed of the network connection to the server
10.  Socket Openeration Test
11.  URL Connection Test URL Connection Test
12.  This server retrieves the time using the RFC867 protocol. This server retrieves the time using the RFC867 protocol.
13.  Quote Server
14.  Client and Server Demo
15.  Reflector








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.