home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / net / ServerSocket.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  3.0 KB  |  81 lines

  1. package java.net;
  2.  
  3. import java.io.FileDescriptor;
  4. import java.io.IOException;
  5.  
  6. public final class ServerSocket {
  7.    SocketImpl impl;
  8.    private static SocketImplFactory factory;
  9.  
  10.    ServerSocket() throws IOException {
  11.       this.impl = (SocketImpl)(factory != null ? factory.createSocketImpl() : new PlainSocketImpl());
  12.    }
  13.  
  14.    public ServerSocket(int port) throws IOException {
  15.       this(port, 50);
  16.    }
  17.  
  18.    public ServerSocket(int port, int count) throws IOException {
  19.       this();
  20.       SecurityManager security = System.getSecurityManager();
  21.       if (security != null) {
  22.          security.checkListen(port);
  23.       }
  24.  
  25.       this.impl.create(true);
  26.       this.impl.bind(InetAddress.anyLocalAddress, port);
  27.       this.impl.listen(count);
  28.    }
  29.  
  30.    public InetAddress getInetAddress() {
  31.       return this.impl.getInetAddress();
  32.    }
  33.  
  34.    public int getLocalPort() {
  35.       return this.impl.getLocalPort();
  36.    }
  37.  
  38.    public Socket accept() throws IOException {
  39.       Socket s = new Socket();
  40.  
  41.       try {
  42.          s.impl.address = new InetAddress();
  43.          s.impl.fd = new FileDescriptor();
  44.          this.impl.accept(s.impl);
  45.          SecurityManager security = System.getSecurityManager();
  46.          if (security != null) {
  47.             security.checkAccept(s.getInetAddress().getHostName(), s.getPort());
  48.          }
  49.  
  50.          return s;
  51.       } catch (IOException e) {
  52.          s.close();
  53.          throw e;
  54.       } catch (SecurityException e) {
  55.          s.close();
  56.          throw e;
  57.       }
  58.    }
  59.  
  60.    public void close() throws IOException {
  61.       this.impl.close();
  62.    }
  63.  
  64.    public String toString() {
  65.       return "ServerSocket[addr=" + this.impl.getInetAddress() + ",port=" + this.impl.getPort() + ",localport=" + this.impl.getLocalPort() + "]";
  66.    }
  67.  
  68.    public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException {
  69.       if (factory != null) {
  70.          throw new SocketException("factory already defined");
  71.       } else {
  72.          SecurityManager security = System.getSecurityManager();
  73.          if (security != null) {
  74.             security.checkSetFactory();
  75.          }
  76.  
  77.          factory = fac;
  78.       }
  79.    }
  80. }
  81.