home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / ServerSocket.java < prev    next >
Text File  |  1996-05-03  |  5KB  |  169 lines

  1. /*
  2.  * @(#)ServerSocket.java    1.18 95/12/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.FileDescriptor;
  24.  
  25. /**
  26.  * The server Socket class. It uses a SocketImpl
  27.  * to implement the actual socket operations. It is done this way 
  28.  * so that you are able to change socket implementations depending 
  29.  * on the kind of firewall being used. You can change socket
  30.  * implementations by setting the SocketImplFactory.
  31.  *
  32.  * @version     1.18, 18 Dec 1995
  33.  * @author     Jonathan Payne
  34.  * @author     Arthur van Hoff
  35.  */
  36. public final 
  37. class ServerSocket {
  38.     /**
  39.      * The implementation of this Socket.
  40.      */
  41.     SocketImpl impl;
  42.  
  43.     /**
  44.      * Creates an unconnected server socket. Note: this method
  45.      * should not be public.
  46.      * @exception IOException IO error when opening the socket.
  47.      */
  48.     ServerSocket() throws IOException {
  49.     impl = (factory != null) ? factory.createSocketImpl() : new PlainSocketImpl();
  50.     }
  51.  
  52.     /**
  53.      * Creates a server socket on a specified port.
  54.      * @param port the port
  55.      * @exception IOException IO error when opening the socket.
  56.      */
  57.     public ServerSocket(int port) throws IOException {
  58.     this(port, 50);
  59.     }
  60.  
  61.     /**
  62.      * Creates a server socket, binds it to the specified local port 
  63.      * and listens to it.  You can connect to an annonymous port by 
  64.      * specifying the port number to be 0.  <i>backlog<\i> specifies
  65.      * how many connection requests the system will queue up while waiting
  66.      * for the ServerSocket to execute accept().
  67.      * @param port the specified port
  68.      * @param backlog the number of queued connect requests pending accept
  69.      */
  70.     public ServerSocket(int port, int backlog) throws IOException {
  71.     this();
  72.  
  73.     SecurityManager security = System.getSecurityManager();
  74.     if (security != null) {
  75.         security.checkListen(port);
  76.     }
  77.  
  78.     impl.create(true);
  79.     impl.bind(InetAddress.anyLocalAddress, port);
  80.     impl.listen(backlog);
  81.     }
  82.  
  83.     /**
  84.      * Gets the address to which the socket is connected.
  85.      */
  86.     public InetAddress getInetAddress() {
  87.     return impl.getInetAddress();
  88.     }
  89.  
  90.     /**
  91.      * Gets the port on which the socket is listening.
  92.      */
  93.     public int getLocalPort() {
  94.     return impl.getLocalPort();
  95.     }
  96.  
  97.     /**
  98.      * Accepts a connection. This method will block until the
  99.      * connection is made.
  100.      * @exception IOException IO error when waiting for the connection.
  101.      */
  102.     public Socket accept() throws IOException {
  103.     Socket s = new Socket();
  104.  
  105.     try {
  106.         //s.impl.create(true);
  107.         s.impl.address = new InetAddress();
  108.         s.impl.fd = new FileDescriptor();
  109.         impl.accept(s.impl);
  110.  
  111.         SecurityManager security = System.getSecurityManager();
  112.         if (security != null) {
  113.         security.checkAccept(s.getInetAddress().getHostName(),
  114.                      s.getPort());
  115.         }
  116.     } catch (IOException e) {
  117.         s.close();
  118.         throw e;
  119.     } catch (SecurityException e) {
  120.         s.close();
  121.         throw e;
  122.     }
  123.     
  124.     return s;
  125.     }
  126.  
  127.     /**
  128.      * Closes the server socket.
  129.      * @exception IOException IO error when closing the socket.
  130.      */
  131.     public void close() throws IOException {
  132.     impl.close();
  133.     }
  134.  
  135.     /**
  136.      * Returns the implementation address and implementation port of 
  137.      * this ServerSocket as a String.
  138.      */
  139.     public String toString() {
  140.     return "ServerSocket[addr=" + impl.getInetAddress() + 
  141.         ",port=" + impl.getPort() + 
  142.         ",localport=" + impl.getLocalPort()  + "]";
  143.     }
  144.  
  145.     /**
  146.      * The factory for all server sockets.
  147.      */
  148.     private static SocketImplFactory factory;
  149.  
  150.     /**
  151.      * Sets the system's server SocketImplFactory. The factory can 
  152.      * be specified only once.
  153.      * @param fac the desired factory
  154.      * @exception SocketException If the factory has already been 
  155.      * defined.
  156.      * @exception IOException IO error when setting the socket factor.
  157.      */
  158.     public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException {
  159.     if (factory != null) {
  160.         throw new SocketException("factory already defined");
  161.     }
  162.     SecurityManager security = System.getSecurityManager();
  163.     if (security != null) {
  164.         security.checkSetFactory();
  165.     }
  166.     factory = fac;
  167.     }
  168. }
  169.