home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / net / plains~1.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  5.4 KB  |  202 lines

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