home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / net / server~1.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  4.7 KB  |  167 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, 12/18/95
  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.
  65.      * @param port the specified port
  66.      * @param count the amountt of time to listen for a connection
  67.      */
  68.     public ServerSocket(int port, int count) throws IOException {
  69.     this();
  70.  
  71.     SecurityManager security = System.getSecurityManager();
  72.     if (security != null) {
  73.         security.checkListen(port);
  74.     }
  75.  
  76.     impl.create(true);
  77.     impl.bind(InetAddress.anyLocalAddress, port);
  78.     impl.listen(count);
  79.     }
  80.  
  81.     /**
  82.      * Gets the address to which the socket is connected.
  83.      */
  84.     public InetAddress getInetAddress() {
  85.     return impl.getInetAddress();
  86.     }
  87.  
  88.     /**
  89.      * Gets the port on which the socket is listening.
  90.      */
  91.     public int getLocalPort() {
  92.     return impl.getLocalPort();
  93.     }
  94.  
  95.     /**
  96.      * Accepts a connection. This method will block until the
  97.      * connection is made.
  98.      * @exception IOException IO error when waiting for the connection.
  99.      */
  100.     public Socket accept() throws IOException {
  101.     Socket s = new Socket();
  102.  
  103.     try {
  104.         //s.impl.create(true);
  105.         s.impl.address = new InetAddress();
  106.         s.impl.fd = new FileDescriptor();
  107.         impl.accept(s.impl);
  108.  
  109.         SecurityManager security = System.getSecurityManager();
  110.         if (security != null) {
  111.         security.checkAccept(s.getInetAddress().getHostName(),
  112.                      s.getPort());
  113.         }
  114.     } catch (IOException e) {
  115.         s.close();
  116.         throw e;
  117.     } catch (SecurityException e) {
  118.         s.close();
  119.         throw e;
  120.     }
  121.     
  122.     return s;
  123.     }
  124.  
  125.     /**
  126.      * Closes the server socket.
  127.      * @exception IOException IO error when closing the socket.
  128.      */
  129.     public void close() throws IOException {
  130.     impl.close();
  131.     }
  132.  
  133.     /**
  134.      * Returns the implementation address and implementation port of 
  135.      * this ServerSocket as a String.
  136.      */
  137.     public String toString() {
  138.     return "ServerSocket[addr=" + impl.getInetAddress() + 
  139.         ",port=" + impl.getPort() + 
  140.         ",localport=" + impl.getLocalPort()  + "]";
  141.     }
  142.  
  143.     /**
  144.      * The factory for all server sockets.
  145.      */
  146.     private static SocketImplFactory factory;
  147.  
  148.     /**
  149.      * Sets the system's server SocketImplFactory. The factory can 
  150.      * be specified only once.
  151.      * @param fac the desired factory
  152.      * @exception SocketException If the factory has already been 
  153.      * defined.
  154.      * @exception IOException IO error when setting the socket factor.
  155.      */
  156.     public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException {
  157.     if (factory != null) {
  158.         throw new SocketException("factory already defined");
  159.     }
  160.     SecurityManager security = System.getSecurityManager();
  161.     if (security != null) {
  162.         security.checkSetFactory();
  163.     }
  164.     factory = fac;
  165.     }
  166. }
  167.