home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / net / NetworkClient.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  2.4 KB  |  76 lines

  1. /*
  2.  * @(#)NetworkClient.java    1.18 95/08/22 Jonathan Payne
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package sun.net;
  20.  
  21. import java.io.*;
  22. import java.net.Socket;
  23. import java.net.UnknownHostException;
  24. import java.net.URL;
  25.  
  26. /**
  27.  * This is the base class for network clients.
  28.  *
  29.  * @version    1.18, 08/22/95
  30.  * @author    Jonathan Payne
  31.  */
  32. public class NetworkClient {
  33.     /** Socket for communicating with server. */
  34.     protected Socket    serverSocket = null;
  35.  
  36.     /** Stream for printing to the server. */
  37.     public PrintStream    serverOutput;
  38.  
  39.     /** Buffered stream for reading replies from server. */
  40.     public InputStream    serverInput;
  41.  
  42.     /** Open a connection to the server. */
  43.     public void openServer(String server, int port)
  44.     throws IOException, UnknownHostException {
  45.     if (serverSocket != null)
  46.         closeServer();
  47.     serverSocket = new Socket(server, port);
  48.     serverOutput = new PrintStream(new BufferedOutputStream(serverSocket.getOutputStream()),
  49.                        true);
  50.     serverInput = new BufferedInputStream(serverSocket.getInputStream());
  51.     }
  52.  
  53.     /** Close an open connection to the server. */
  54.     public void closeServer() throws IOException {
  55.     if (! serverIsOpen()) {
  56.         return;
  57.     }
  58.     serverSocket.close();
  59.     serverSocket = null;
  60.     serverInput = null;
  61.     serverOutput = null;
  62.     }
  63.  
  64.     /** Return server connection status */
  65.     public boolean serverIsOpen() {
  66.     return serverSocket != null;
  67.     }
  68.  
  69.     /** Create connection with host <i>host</i> on port <i>port</i> */
  70.     public NetworkClient(String host, int port) throws IOException {
  71.     openServer(host, port);
  72.     }
  73.  
  74.     public NetworkClient() {}
  75. }
  76.