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

  1. /*
  2.  * @(#)PlainSocketImpl.java    1.11 95/10/18 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.IOException;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.io.InterruptedIOException;
  26.  
  27. /**
  28.  * Default Socket Implementation. This implementation does
  29.  * not implement any security checks neither does it support
  30.  * any fire walls. Note this class should <b>NOT</b> be public.
  31.  *
  32.  * @version     1.11, 10/18/95
  33.  * @author     Jonathan Payne
  34.  * @author     Arthur van Hoff
  35.  */
  36. class PlainSocketImpl extends SocketImpl
  37. {
  38.     /**
  39.      * Creates a socket with a boolean that specifies whether this
  40.      * is a stream socket or a datagram socket.
  41.      * @param stream the stream to be created
  42.      */
  43.     protected synchronized void create(boolean stream) throws IOException {
  44.     socketCreate(stream);
  45.     }
  46.  
  47.     /** 
  48.      * Creates a socket and connects it to the specified port on
  49.      * the specified host.
  50.      * @param host the specified host
  51.      * @param port the specified port 
  52.      */
  53.     protected void connect(String host, int port)
  54.         throws UnknownHostException, IOException
  55.     {
  56.     IOException pending = null;
  57.     try {
  58.         InetAddress address = InetAddress.getByName(host);
  59.  
  60.         for (int i = 0 ; i < 3 ; i++) {
  61.         try {
  62.             socketConnect(address, port);
  63.             return;
  64.         } catch (ProtocolException e) {
  65.             // Try again in case of a protocol exception
  66.             socketClose();
  67.             socketCreate(true);
  68.             pending = e;
  69.         } catch (IOException e) {
  70.             // Let someone else deal with this exception
  71.             socketClose();
  72.             throw e;
  73.         }
  74.         }
  75.     } catch (UnknownHostException e) {
  76.         pending = e;
  77.     }
  78.  
  79.     // everything failed
  80.     socketClose();
  81.     throw pending;
  82.     }
  83.  
  84.     /** 
  85.      * Creates a socket and connects it to the specified address on
  86.      * the specified port.
  87.      * @param address the address
  88.      * @param port the specified port
  89.      */
  90.     protected void connect(InetAddress address, int port) throws IOException {
  91.     this.port = port;
  92.     this.address = address;
  93.  
  94.     ProtocolException pending = null;
  95.     for (int i = 0 ; i < 3 ; i++) {
  96.         try {
  97.         socketConnect(address, port);
  98.         return;
  99.         } catch (ProtocolException e) {
  100.         // Try again in case of a protocol exception
  101.         socketClose();
  102.         socketCreate(true);
  103.         pending = e;
  104.         } catch (IOException e) {
  105.         // Let someone else deal with this exception
  106.         socketClose();
  107.         throw e;
  108.         }
  109.     }
  110.     // everything failed
  111.     socketClose();
  112.     throw pending;
  113.     }
  114.  
  115.     /**
  116.      * Binds the socket to the specified address of the specified local port.
  117.      * @param address the address
  118.      * @param port the port
  119.      */
  120.     protected synchronized void bind(InetAddress address, int lport) 
  121.     throws IOException
  122.     {
  123.     socketBind(address, lport);
  124.     }
  125.  
  126.     /**
  127.      * Listens, for a specified amount of time, for connections.
  128.      * @param count the amount of time to listen for connections
  129.      */
  130.     protected synchronized void listen(int count) throws IOException {
  131.     socketListen(count);
  132.     }
  133.  
  134.     /**
  135.      * Accepts connections.
  136.      * @param s the connection
  137.      */
  138.     protected synchronized void accept(SocketImpl s) throws IOException {
  139.     socketAccept(s);
  140.     }
  141.  
  142.     /**
  143.      * Gets an InputStream for this socket.
  144.      */
  145.     protected synchronized InputStream getInputStream() throws IOException {
  146.     return new SocketInputStream(this);
  147.     }
  148.  
  149.     /**
  150.      * Gets an OutputStream for this socket.
  151.      */
  152.     protected synchronized OutputStream getOutputStream() throws IOException {
  153.     return new SocketOutputStream(this);
  154.     }
  155.  
  156.     /**
  157.      * Closes the socket.
  158.      */
  159.     protected synchronized void close() throws IOException {
  160.     socketClose();
  161.     }
  162.  
  163.     /**
  164.      * Cleans up if the user forgets to close it.
  165.      */
  166.     protected synchronized void finalize() throws IOException {
  167.     socketClose();
  168.     }
  169.  
  170.     private native void socketCreate(boolean stream) throws IOException;
  171.     private native void socketConnect(InetAddress address, int port)
  172.     throws IOException;
  173.     private native void socketBind(InetAddress address, int port)
  174.     throws IOException;
  175.     private native void socketListen(int count)
  176.     throws IOException;
  177.     private native void socketAccept(SocketImpl s)
  178.     throws IOException;
  179.     private native void socketClose()
  180.     throws IOException;
  181. }
  182.