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[] args) throws Exception {
Socket sock = new Socket(args[0], 1234);
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
Hashtable hash = (Hashtable) ois.readObject();
System.out.println(hash);
ois.close();
sock.close();
}
}
Related examples in the same category