home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / net / SocketImpl.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  6.3 KB  |  200 lines

  1. /*
  2.  * @(#)SocketImpl.java    1.24 98/03/18
  3.  *
  4.  * Copyright 1995-1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.net;
  16.  
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.io.FileDescriptor;
  21.  
  22. /**
  23.  * The abstract class <code>SocketImpl</code> is a common superclass 
  24.  * of all classes that actually implement sockets. It is used to 
  25.  * create both client and server sockets. 
  26.  * <p>
  27.  * A "plain" socket implements these methods exactly as 
  28.  * described, without attempting to go through a firewall or proxy. 
  29.  *
  30.  * @author  unascribed
  31.  * @version 1.24, 03/18/98
  32.  * @since   JDK1.0
  33.  */
  34. public abstract class SocketImpl implements SocketOptions {
  35.     /**
  36.      * The file descriptor object for this socket. 
  37.      */
  38.     protected FileDescriptor fd;
  39.     
  40.     /**
  41.      * The IP address of the remote end of this socket. 
  42.      */
  43.     protected InetAddress address;
  44.    
  45.     /**
  46.      * The port number on the remote host to which this socket is connected. 
  47.      */
  48.     protected int port;
  49.  
  50.     /**
  51.      * The local port number to which this socket is connected. 
  52.      */
  53.     protected int localport;   
  54.  
  55.     /**
  56.      * Creates either a stream or a datagram socket. 
  57.      *
  58.      * @param      stream   if <code>true</code>, create a stream socket;
  59.      *                      otherwise, create a datagram socket.
  60.      * @exception  IOException  if an I/O error occurs while creating the
  61.      *               socket.
  62.      */
  63.     protected abstract void create(boolean stream) throws IOException;
  64.  
  65.     /**
  66.      * Connects this socket to the specified port on the named host. 
  67.      *
  68.      * @param      host   the name of the remote host.
  69.      * @param      port   the port number.
  70.      * @exception  IOException  if an I/O error occurs when connecting to the
  71.      *               remote host.
  72.      */
  73.     protected abstract void connect(String host, int port) throws IOException;
  74.  
  75.     /**
  76.      * Connects this socket to the specified port number on the specified host.
  77.      *
  78.      * @param      address   the IP address of the remote host.
  79.      * @param      port      the port number.
  80.      * @exception  IOException  if an I/O error occurs when attempting a
  81.      *               connection.
  82.      */
  83.     protected abstract void connect(InetAddress address, int port) throws IOException;
  84.  
  85.     /**
  86.      * Binds this socket to the specified port number on the specified host. 
  87.      *
  88.      * @param      host   the IP address of the remote host.
  89.      * @param      port   the port number.
  90.      * @exception  IOException  if an I/O error occurs when binding this socket.
  91.      */
  92.     protected abstract void bind(InetAddress host, int port) throws IOException;
  93.  
  94.     /**
  95.      * Sets the maximum queue length for incoming connection indications 
  96.      * (a request to connect) to the <code>count</code> argument. If a 
  97.      * connection indication arrives when the queue is full, the 
  98.      * connection is refused. 
  99.      *
  100.      * @param      backlog   the maximum length of the queue.
  101.      * @exception  IOException  if an I/O error occurs when creating the queue.
  102.      */
  103.     protected abstract void listen(int backlog) throws IOException;
  104.  
  105.     /**
  106.      * Accepts a connection. 
  107.      *
  108.      * @param      s   the accepted connection.
  109.      * @exception  IOException  if an I/O error occurs when accepting the
  110.      *               connection.
  111.      */
  112.     protected abstract void accept(SocketImpl s) throws IOException;
  113.  
  114.     /**
  115.      * Returns an input stream for this socket.
  116.      *
  117.      * @return     a stream for reading from this socket.
  118.      * @exception  IOException  if an I/O error occurs when creating the
  119.      *               input stream.
  120.     */
  121.     protected abstract InputStream getInputStream() throws IOException;
  122.  
  123.     /**
  124.      * Returns an output stream for this socket.
  125.      *
  126.      * @return     an output stream for writing to this socket.
  127.      * @exception  IOException  if an I/O error occurs when creating the
  128.      *               output stream.
  129.      */
  130.     protected abstract OutputStream getOutputStream() throws IOException;
  131.  
  132.     /**
  133.      * Returns the number of bytes that can be read from this socket
  134.      * without blocking.
  135.      *
  136.      * @return     the number of bytes that can be read from this socket
  137.      *             without blocking.
  138.      * @exception  IOException  if an I/O error occurs when determining the
  139.      *               number of bytes available.
  140.      */
  141.     protected abstract int available() throws IOException;
  142.  
  143.     /**
  144.      * Closes this socket. 
  145.      *
  146.      * @exception  IOException  if an I/O error occurs when closing this socket.
  147.      */
  148.     protected abstract void close() throws IOException;
  149.  
  150.     /**
  151.      * Returns the value of this socket's <code>fd</code> field.
  152.      *
  153.      * @return  the value of this socket's <code>fd</code> field.
  154.      * @see     java.net.SocketImpl#fd
  155.      */
  156.     protected FileDescriptor getFileDescriptor() {
  157.     return fd;
  158.     }
  159.  
  160.     /**
  161.      * Returns the value of this socket's <code>address</code> field.
  162.      *
  163.      * @return  the value of this socket's <code>address</code> field.
  164.      * @see     java.net.SocketImpl#address
  165.      */
  166.     protected InetAddress getInetAddress() {
  167.     return address;
  168.     }
  169.  
  170.     /**
  171.      * Returns the value of this socket's <code>port</code> field.
  172.      *
  173.      * @return  the value of this socket's <code>port</code> field.
  174.      * @see     java.net.SocketImpl#port
  175.      */
  176.     protected int getPort() {
  177.     return port;
  178.     }
  179.  
  180.     /**
  181.      * Returns the value of this socket's <code>localport</code> field.
  182.      *
  183.      * @return  the value of this socket's <code>localport</code> field.
  184.      * @see     java.net.SocketImpl#localport
  185.      */
  186.     protected int getLocalPort() {
  187.     return localport;
  188.     }
  189.     
  190.     /**
  191.      * Returns the address and port of this socket as a <code>String</code>.
  192.      *
  193.      * @return  a string representation of this socket.
  194.      */
  195.     public String toString() {
  196.     return "Socket[addr=" + getInetAddress() +
  197.         ",port=" + getPort() + ",localport=" + getLocalPort()  + "]";
  198.     }
  199. }
  200.