home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / SocketImpl.java < prev    next >
Text File  |  1997-05-20  |  8KB  |  231 lines

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