FileDocCategorySizeDatePackage
Server.javaAPI DocExample2038Wed Apr 19 11:21:14 BST 2000None

Server

public class Server extends Object
This example shows how to use sockets to send and receive objects. This file contains the class Server, which does the receiving of objects from class WriteSocket in file WriteSocket.java The Server has to run first and wait for the WriteSocket to send the information. Compiled and Tested with JDK1.1 & JDK1.2

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)
Create the serversocket and use its stream to receive serialized objects


      ServerSocket ser = null;
      Socket soc = null;
      String str = null;
      Date d = null;
   
      try {
	ser = new ServerSocket(8020);
	/*
	 * This will wait for a connection to be made to this socket.
         */	
	soc = ser.accept();
	InputStream o = soc.getInputStream();
	ObjectInput s = new ObjectInputStream(o);
	str = (String) s.readObject();
	d = (Date) s.readObject();
	s.close();
	
	// print out what we just received
	System.out.println(str);
	System.out.println(d);
      } catch (Exception e) {
       	  System.out.println(e.getMessage());
	  System.out.println("Error during serialization");
	  System.exit(1);
      }