home *** CD-ROM | disk | FTP | other *** search
- package java.net;
-
- import java.io.FileDescriptor;
- import java.io.IOException;
-
- public final class ServerSocket {
- SocketImpl impl;
- private static SocketImplFactory factory;
-
- ServerSocket() throws IOException {
- this.impl = (SocketImpl)(factory != null ? factory.createSocketImpl() : new PlainSocketImpl());
- }
-
- public ServerSocket(int port) throws IOException {
- this(port, 50);
- }
-
- public ServerSocket(int port, int count) throws IOException {
- this();
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkListen(port);
- }
-
- this.impl.create(true);
- this.impl.bind(InetAddress.anyLocalAddress, port);
- this.impl.listen(count);
- }
-
- public InetAddress getInetAddress() {
- return this.impl.getInetAddress();
- }
-
- public int getLocalPort() {
- return this.impl.getLocalPort();
- }
-
- public Socket accept() throws IOException {
- Socket s = new Socket();
-
- try {
- s.impl.address = new InetAddress();
- s.impl.fd = new FileDescriptor();
- this.impl.accept(s.impl);
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkAccept(s.getInetAddress().getHostName(), s.getPort());
- }
-
- return s;
- } catch (IOException e) {
- s.close();
- throw e;
- } catch (SecurityException e) {
- s.close();
- throw e;
- }
- }
-
- public void close() throws IOException {
- this.impl.close();
- }
-
- public String toString() {
- return "ServerSocket[addr=" + this.impl.getInetAddress() + ",port=" + this.impl.getPort() + ",localport=" + this.impl.getLocalPort() + "]";
- }
-
- public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException {
- if (factory != null) {
- throw new SocketException("factory already defined");
- } else {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkSetFactory();
- }
-
- factory = fac;
- }
- }
- }
-