home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / net / socket.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  5.4 KB  |  203 lines

  1. /*
  2.  * @(#)Socket.java    1.13 95/10/24 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.  
  20. package java.net;
  21.  
  22. import java.io.InputStream;
  23. import java.io.OutputStream;
  24. import java.io.IOException;
  25. import java.io.InterruptedIOException;
  26.  
  27. /**
  28.  * The client Socket class. It uses a SocketImpl
  29.  * to implement the actual socket operations. It is done this way 
  30.  * so that you are able to change socket implementations depending 
  31.  * on the kind of firewall that is used. You can change socket
  32.  * implementations by setting the SocketImplFactory.
  33.  *
  34.  * @version     1.13, 10/24/95
  35.  * @author     Jonathan Payne
  36.  * @author     Arthur van Hoff
  37.  */
  38. public final 
  39. class Socket {
  40.     /**
  41.      * The implementation of this Socket.
  42.      */
  43.     SocketImpl impl;
  44.  
  45.     /**
  46.      * Creates an unconnected socket. Note: this method
  47.      * should not be public.
  48.      */
  49.     Socket() {
  50.     impl = (factory != null) ? factory.createSocketImpl() : new PlainSocketImpl();
  51.     }
  52.  
  53.     /** 
  54.      * Creates a stream socket and connects it to the specified port on
  55.      * the specified host.
  56.      * @param host the host
  57.      * @param port the port
  58.      */
  59.     public Socket(String host, int port)
  60.     throws UnknownHostException, IOException
  61.     {
  62.     this(host, port, true);
  63.     }
  64.  
  65.     /** 
  66.      * Creates a socket and connects it to the specified port on
  67.      * the specified host. The last argument lets you specify whether
  68.      * you want a stream or datagram socket.
  69.      * @param host the specified host
  70.      * @param port the specified port
  71.      * @param stream a boolean indicating whether this is a stream 
  72.      * or datagram socket
  73.      */
  74.     public Socket(String host, int port, boolean stream) throws IOException {
  75.     this();
  76.  
  77.     SecurityManager security = System.getSecurityManager();
  78.     if (security != null) {
  79.         security.checkConnect(host, port);
  80.     }
  81.  
  82.     try {
  83.         impl.create(stream);
  84.         impl.connect(host, port);
  85.     } catch (IOException e) {
  86.         impl.close();
  87.         throw e;
  88.     }
  89.     }
  90.  
  91.     /** 
  92.      * Creates a stream socket and connects it to the specified address on
  93.      * the specified port. 
  94.      * @param address the specified address
  95.      * @param port the specified port
  96.      */
  97.     public Socket(InetAddress address, int port) throws IOException {
  98.     this(address, port, true);
  99.     }
  100.  
  101.     /** 
  102.      * Creates a socket and connects it to the specified address on
  103.      * the specified port. The last argument lets you specify whether
  104.      * you want a stream or datagram socket.
  105.      * @param address the specified address
  106.      * @param port the specified port
  107.      * @param stream a boolean indicating whether this is a stream 
  108.      * or datagram socket
  109.      */
  110.     public Socket(InetAddress address, int port, boolean stream) 
  111.     throws IOException
  112.     {
  113.     this();
  114.  
  115.     SecurityManager security = System.getSecurityManager();
  116.     if (security != null) {
  117.         security.checkConnect(address.getHostName(), port);
  118.     }
  119.  
  120.     try {
  121.         impl.create(stream);
  122.         impl.connect(address, port);
  123.     } catch (SocketException e) {
  124.         impl.close();
  125.         throw e;
  126.     }
  127.     }
  128.  
  129.     /**
  130.      * Gets the address to which the socket is connected.
  131.      */
  132.     public InetAddress getInetAddress() {
  133.     return impl.address;
  134.     }
  135.  
  136.     /**
  137.      * Gets the remote port to which the socket is connected.
  138.      */
  139.     public int getPort() {
  140.     return impl.port;
  141.     }
  142.  
  143.     /**
  144.      * Gets the local port to which the socket is connected.
  145.      */
  146.     public int getLocalPort() {
  147.     return impl.localport;
  148.     }
  149.  
  150.     /**
  151.      * Gets an InputStream for this socket.
  152.      */
  153.     public InputStream getInputStream() throws IOException {
  154.     return impl.getInputStream();
  155.     }
  156.  
  157.     /**
  158.      * Gets an OutputStream for this socket.
  159.      */
  160.     public OutputStream getOutputStream() throws IOException {
  161.     return impl.getOutputStream();
  162.     }
  163.  
  164.     /**
  165.      * Closes the socket.
  166.      */
  167.     public synchronized void close() throws IOException {
  168.     impl.close();
  169.     }
  170.  
  171.     /**
  172.      * Converts the Socket to a String.
  173.      */
  174.     public String toString() {
  175.     return "Socket[fd=" + (impl.fd-1) + ",addr=" + impl.address +
  176.         ",port=" + impl.port + ",localport=" + impl.localport + "]";
  177.     }
  178.  
  179.     /**
  180.      * The factory for all client sockets.
  181.      */
  182.     private static SocketImplFactory factory;
  183.  
  184.     /**
  185.      * Sets the system's client SocketImplFactory. The factory can 
  186.      * be specified only once.
  187.      * @param fac the desired factory
  188.      * @exception SocketException If the factory is already defined.
  189.      */
  190.     public static synchronized void setSocketImplFactory(SocketImplFactory fac)
  191.     throws IOException
  192.     {
  193.     if (factory != null) {
  194.         throw new SocketException("factory already defined");
  195.     }
  196.     SecurityManager security = System.getSecurityManager();
  197.     if (security != null) {
  198.         security.checkSetFactory();
  199.     }
  200.     factory = fac;
  201.     }
  202. }
  203.