home *** CD-ROM | disk | FTP | other *** search
/ MACD 4 / MACD4.iso / Internet / Java / networking / sockets / example / EchoTest.java < prev    next >
Encoding:
Java Source  |  1978-03-06  |  2.1 KB  |  55 lines

  1. /*
  2.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.io.*;
  18. import java.net.*;
  19.  
  20. public class EchoTest {
  21.     public static void main(String[] args) {
  22.         Socket echoSocket = null;
  23.         DataOutputStream os = null;
  24.         DataInputStream is = null;
  25.     DataInputStream stdIn = new DataInputStream(System.in);
  26.  
  27.         try {
  28.             echoSocket = new Socket("taranis", 7);
  29.             os = new DataOutputStream(echoSocket.getOutputStream());
  30.             is = new DataInputStream(echoSocket.getInputStream());
  31.         } catch (UnknownHostException e) {
  32.             System.err.println("Don't know about host: taranis");
  33.         } catch (IOException e) {
  34.             System.err.println("Couldn't get I/O for the connection to: taranis");
  35.         }
  36.  
  37.         if (echoSocket != null && os != null && is != null) {
  38.             try {
  39.                 String userInput;
  40.  
  41.                 while ((userInput = stdIn.readLine()) != null) {
  42.                     os.writeBytes(userInput);
  43.                     os.writeByte('\n');
  44.                     System.out.println("echo: " + is.readLine());
  45.                 }
  46.                 os.close();
  47.                 is.close();
  48.                 echoSocket.close();
  49.             } catch (IOException e) {
  50.                 System.err.println("I/O failed on the connection to: taranis");
  51.             }
  52.         }
  53.     }
  54. }
  55.