Serve entire objects using ObjectOutputStream : Server « Network Protocol « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Network Protocol » ServerScreenshots 
Serve entire objects using ObjectOutputStream
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 6123Server allows connections on socket 6123
3.This server displays messages to a single clientThis server displays messages to a single client
4.The client can specify information to control the output of a serverThe client can specify information to control the output of a server
5.A server can use specialized streams to deliver typed dataA server can use specialized streams to deliver typed data
6.A multithreaded serverA 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.This server retrieves the time using the RFC867 protocol.This server retrieves the time using the RFC867 protocol.
11.Quote Server
12.Logging Server based on SocketServer
13.Client and Server Demo
14.Reflector
15.Simple Http Server
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.