home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 October / INEWS_10_CD.ISO / pc / jdk / jdk1.2.2 / docs / guide / serialization / examples / sockets / Server.java < prev    next >
Encoding:
Java Source  |  1999-09-19  |  2.0 KB  |  70 lines

  1. /*
  2.  * @(#)Server.java    1.1 98/10/03        
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  * 
  20.  */
  21.  
  22. import java.io.*;
  23. import java.net.*;
  24. import java.util.*;
  25.  
  26.  
  27. /**
  28.  * This example shows how to use sockets to send and receive objects.
  29.  * This file contains the class Server, which does the receiving of objects 
  30.  * from class WriteSocket in file WriteSocket.java
  31.  * The Server has to run first and wait for the WriteSocket
  32.  * to send the information.
  33.  *
  34.  * Compiled and Tested with JDK1.1 & JDK1.2
  35.  */
  36. public class Server {
  37.  
  38.     /**
  39.      * Create the serversocket and use its stream to receive serialized objects
  40.      */
  41.     public static void main(String args[]) {
  42.  
  43.       ServerSocket ser = null;
  44.       Socket soc = null;
  45.       String str = null;
  46.       Date d = null;
  47.    
  48.       try {
  49.     ser = new ServerSocket(8020);
  50.     /*
  51.      * This will wait for a connection to be made to this socket.
  52.          */    
  53.     soc = ser.accept();
  54.     InputStream o = soc.getInputStream();
  55.     ObjectInput s = new ObjectInputStream(o);
  56.     str = (String) s.readObject();
  57.     d = (Date) s.readObject();
  58.     s.close();
  59.     
  60.     // print out what we just received
  61.     System.out.println(str);
  62.     System.out.println(d);
  63.       } catch (Exception e) {
  64.              System.out.println(e.getMessage());
  65.       System.out.println("Error during serialization");
  66.       System.exit(1);
  67.       }
  68.     }
  69. }
  70.