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 / Client.java next >
Encoding:
Java Source  |  1999-09-19  |  1.6 KB  |  59 lines

  1. /*
  2.  * @(#)Client.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.  * This example shows how to use sockets and serialization to send 
  28.  * objects that will be received
  29.  * (see class Server to see the receiving part)
  30.  *
  31.  * Compiled and Tested with JDK1.1 & JDK1.2
  32.  */
  33. public class Client {
  34.  
  35.     public static void main(String args[]) {
  36.  
  37.     
  38.       try {
  39.       // Create a socket  
  40.       Socket soc = new Socket(InetAddress.getLocalHost(), 8020);
  41.  
  42.       // Serialize today's date to a outputstream associated to the socket
  43.       OutputStream o = soc.getOutputStream();
  44.       ObjectOutput s = new ObjectOutputStream(o);
  45.       
  46.       s.writeObject("Today's date");
  47.       s.writeObject(new Date());
  48.       s.flush(); 
  49.       s.close();
  50.       } catch (Exception e) {
  51.       System.out.println(e.getMessage());
  52.       System.out.println("Error during serialization");
  53.       System.exit(1);
  54.       }
  55.   }
  56. }
  57.  
  58.  
  59.