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

  1. /*
  2.  * @(#)ServerSocket.java    1.27 97/02/10
  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.FileDescriptor;
  27.  
  28. /**
  29.  * This class implements server sockets. A server socket waits for 
  30.  * requests to come in over the network. It performs some operation 
  31.  * based on that request, and then possibly returns a result to the requester.
  32.  * <p>
  33.  * The actual work of the server socket is performed by an instance 
  34.  * of the <code>SocketImpl</code> class. An application can 
  35.  * change the socket factory that creates the socket 
  36.  * implementation to configure itself to create sockets 
  37.  * appropriate to the local firewall. 
  38.  *
  39.  * @author  unascribed
  40.  * @version 1.27, 02/10/97
  41.  * @see     java.net.SocketImpl
  42.  * @see     java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
  43.  * @since   JDK1.0
  44.  */
  45. public 
  46. class ServerSocket {
  47.     /**
  48.      * The implementation of this Socket.
  49.      */
  50.     private SocketImpl impl;
  51.  
  52.     /**
  53.      * Creates an unconnected server socket. Note: this method
  54.      * should not be public.
  55.      * @exception IOException IO error when opening the socket.
  56.      */
  57.     private ServerSocket() throws IOException {
  58.     impl = (factory != null) ? factory.createSocketImpl() : 
  59.         new PlainSocketImpl();
  60.     }
  61.  
  62.     /**
  63.      * Creates a server socket on a specified port. A port of 
  64.      * <code>0</code> creates a socket on any free port. 
  65.      * <p>
  66.      * The maximum queue length for incoming connection indications (a 
  67.      * request to connect) is set to <code>50</code>. If a connection 
  68.      * indication arrives when the queue is full, the connection is refused.
  69.      * <p>
  70.      * If the application has specified a server socket factory, that 
  71.      * factory's <code>createSocketImpl</code> method is called to create 
  72.      * the actual socket implementation. Otherwise a "plain" socket is created.
  73.      *
  74.      * @param      port  the port number, or <code>0</code> to use any
  75.      *                   free port.
  76.      * @exception  IOException  if an I/O error occurs when opening the socket.
  77.      * @see        java.net.SocketImpl
  78.      * @see        java.net.SocketImplFactory#createSocketImpl()
  79.      * @see        java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
  80.      * @since      JDK1.0
  81.      */
  82.     public ServerSocket(int port) throws IOException {
  83.     this(port, 50, null);
  84.     }
  85.  
  86.     /**
  87.      * Creates a server socket and binds it to the specified local port 
  88.      * number. A port number of <code>0</code> creates a socket on any 
  89.      * free port. 
  90.      * <p>
  91.      * The maximum queue length for incoming connection indications (a 
  92.      * request to connect) is set to the <code>count</code> parameter. If 
  93.      * a connection indication arrives when the queue is full, the 
  94.      * connection is refused. 
  95.      * <p>
  96.      * If the application has specified a server socket factory, that 
  97.      * factory's <code>createSocketImpl</code> method is called to create 
  98.      * the actual socket implementation. Otherwise a "plain" socket is created.
  99.      *
  100.      * @param      port     the specified port, or <code>0</code> to use
  101.      *                      any free port.
  102.      * @param      backlog  the maximum length of the queue.
  103.      * @exception  IOException  if an I/O error occurs when opening the socket.
  104.      * @see        java.net.SocketImpl
  105.      * @see        java.net.SocketImplFactory#createSocketImpl()
  106.      * @see        java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
  107.      * @since      JDK1.0
  108.      */
  109.     public ServerSocket(int port, int backlog) throws IOException {
  110.     this(port, backlog, null);
  111.     }
  112.  
  113.     /** 
  114.      * Create a server with the specified port, listen backlog, and 
  115.      * local IP address to bind to.  The <i>bindAddr</i> argument
  116.      * can be used on a multi-homed host for a ServerSocket that
  117.      * will only accept connect requests to one of its addresses.
  118.      * If <i>bindAddr</i> is null, it will default accepting
  119.      * connections on any/all local addresses.
  120.      * The port must be between 0 and 65535, inclusive.
  121.      * <P>
  122.      * @param port the local TCP port
  123.      * @param backlog the listen backlog
  124.      * @param bindAddr the local InetAddress the server will bind to
  125.      * @see SocketConstants
  126.      * @see SocketOption
  127.      * @see SocketImpl
  128.      * @see   JDK1.1
  129.      */
  130.     public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException {
  131.         this();
  132.  
  133.     if (port < 0 || port > 0xFFFF)
  134.         throw new IllegalArgumentException(
  135.                "Port value out of range: " + port);
  136.     try {
  137.         SecurityManager security = System.getSecurityManager();
  138.         if (security != null) {
  139.         security.checkListen(port);
  140.         }
  141.  
  142.         impl.create(true); // a stream socket
  143.         if (bindAddr == null)
  144.         bindAddr = InetAddress.anyLocalAddress;    
  145.  
  146.         impl.bind(bindAddr, port);
  147.         impl.listen(backlog);
  148.  
  149.     } catch(SecurityException e) {
  150.         impl.close();
  151.         throw e;
  152.     } catch(IOException e) {
  153.         impl.close();
  154.         throw e;
  155.     }
  156.     }
  157.  
  158.     /**
  159.      * Returns the local address of this server socket.
  160.      *
  161.      * @return  the address to which this socket is connected,
  162.      *          or <code>null</code> if the socket is not yet connected.
  163.      * @since   JDK1.0
  164.      */
  165.     public InetAddress getInetAddress() {
  166.     return impl.getInetAddress();
  167.     }
  168.  
  169.     /**
  170.      * Returns the port on which this socket is listening.
  171.      *
  172.      * @return  the port number to which this socket is listening.
  173.      * @since   JDK1.0
  174.      */
  175.     public int getLocalPort() {
  176.     return impl.getLocalPort();
  177.     }
  178.  
  179.     /**
  180.      * Listens for a connection to be made to this socket and accepts 
  181.      * it. The method blocks until a connection is made. 
  182.      *
  183.      * @exception  IOException  if an I/O error occurs when waiting for a
  184.      *               connection.
  185.      * @since      JDK1.0
  186.      */
  187.     public Socket accept() throws IOException {
  188.     Socket s = new Socket();
  189.     implAccept(s);
  190.     return s;
  191.     }
  192.  
  193.     /**
  194.      * Subclasses of ServerSocket use this method to override accept()
  195.      * to return their own subclass of socket.  So a FooServerSocket
  196.      * will typically hand this method an <i>empty</i> FooSocket().  On
  197.      * return from implAccept the FooSocket will be connected to a client.
  198.      *
  199.      * @since   JDk1.1
  200.      */
  201.     protected final void implAccept(Socket s) throws IOException {
  202.     try {
  203.         //s.impl.create(true);
  204.         s.impl.address = new InetAddress();
  205.         s.impl.fd = new FileDescriptor();
  206.         impl.accept(s.impl);
  207.         
  208.         SecurityManager security = System.getSecurityManager();
  209.         if (security != null) {
  210.         security.checkAccept(s.getInetAddress().getHostAddress(),
  211.                      s.getPort());
  212.         }
  213.     } catch (IOException e) {
  214.         s.close();
  215.         throw e;
  216.     } catch (SecurityException e) {
  217.         s.close();
  218.         throw e;
  219.     }
  220.     }
  221.  
  222.     /**
  223.      * Closes this socket. 
  224.      *
  225.      * @exception  IOException  if an I/O error occurs when closing the socket.
  226.      * @since      JDK1.0
  227.      */
  228.     public void close() throws IOException {
  229.     impl.close();
  230.     }
  231.  
  232.     /** Enable/disable SO_TIMEOUT with the specified timeout, in
  233.      *  milliseconds.  With this option set to a non-zero timeout,
  234.      *  a call to accept() for this ServerSocket
  235.      *  will block for only this amount of time.  If the timeout expires,
  236.      *  a <B>java.io.InterruptedIOException</B> is raised, though the
  237.      *  ServerSocket is still valid.  The option <B>must</B> be enabled
  238.      *  prior to entering the blocking operation to have effect.  The 
  239.      *  timeout must be > 0.
  240.      *  A timeout of zero is interpreted as an infinite timeout.  
  241.      *
  242.      * @since   JDK1.1
  243.      */
  244.     public synchronized void setSoTimeout(int timeout) throws SocketException {
  245.     impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
  246.     }
  247.  
  248.     /** Retrive setting for SO_TIMEOUT.  0 returns implies that the
  249.      *  option is disabled (i.e., timeout of infinity).
  250.      *
  251.      * @since   JDK1.1
  252.      */
  253.     public synchronized int getSoTimeout() throws IOException {
  254.     Object o = impl.getOption(SocketOptions.SO_TIMEOUT);
  255.     /* extra type safety */
  256.     if (o instanceof Integer) {
  257.         return ((Integer) o).intValue();
  258.     } else {
  259.         return 0;
  260.     }
  261.     }
  262.  
  263.     /**
  264.      * Returns the implementation address and implementation port of 
  265.      * this socket as a <code>String</code>.
  266.      *
  267.      * @return  a string representation of this socket.
  268.      * @since   JDK1.0
  269.      */
  270.     public String toString() {
  271.     return "ServerSocket[addr=" + impl.getInetAddress() + 
  272.         ",port=" + impl.getPort() + 
  273.         ",localport=" + impl.getLocalPort()  + "]";
  274.     }
  275.  
  276.     /**
  277.      * The factory for all server sockets.
  278.      */
  279.     private static SocketImplFactory factory;
  280.  
  281.     /**
  282.      * Sets the server socket implementation factory for the 
  283.      * application. The factory can be specified only once. 
  284.      * <p>
  285.      * When an application creates a new server socket, the socket 
  286.      * implementation factory's <code>createSocketImpl</code> method is 
  287.      * called to create the actual socket implementation. 
  288.      *
  289.      * @param      fac   the desired factory.
  290.      * @exception  IOException  if an I/O error occurs when setting the
  291.      *               socket factory.
  292.      * @exception  SocketException  if the factory has already been defined.
  293.      * @see        java.net.SocketImplFactory#createSocketImpl()
  294.      * @since      JDK1.0
  295.      */
  296.     public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException {
  297.     if (factory != null) {
  298.         throw new SocketException("factory already defined");
  299.     }
  300.     SecurityManager security = System.getSecurityManager();
  301.     if (security != null) {
  302.         security.checkSetFactory();
  303.     }
  304.     factory = fac;
  305.     }
  306. }
  307.