OSDN Git Service

1999-05-26 Bryce McKinlay <bryce@albatross.co.nz>
[pf3gnuchains/gcc-fork.git] / libjava / java / net / ServerSocket.java
1 // ServerSocket.java
2
3 /* Copyright (C) 1999  Cygnus Solutions
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 /**
12   * @author Per Bothner <bothner@cygnus.com>
13   * @date January 6, 1999.
14   */
15
16 /** Written using on-line Java Platform 1.2 API Specification.
17   * Status:  I believe all methods are implemented.
18   */
19
20 package java.net;
21 import java.io.*;
22
23 public class ServerSocket
24 {
25   static SocketImplFactory factory;
26   SocketImpl impl;
27
28   public ServerSocket (int port)
29     throws java.io.IOException
30   {
31     this(port, 5);
32   }
33
34   public ServerSocket (int port, int backlog)
35     throws java.io.IOException
36   {
37     this(port, backlog, InetAddress.getLocalHost());
38   }
39
40   public ServerSocket (int port, int backlog, InetAddress bindAddr)
41     throws java.io.IOException
42   {
43     if (factory == null)
44       this.impl = new PlainSocketImpl();
45     else
46       this.impl = factory.createSocketImpl();
47     SecurityManager s = System.getSecurityManager();
48     if (s != null)
49       s.checkListen(port);
50     impl.create(true);
51     impl.bind(bindAddr, port);
52     impl.listen(backlog);
53   }
54
55   public InetAddress getInetAddress()
56   {
57     return impl.getInetAddress();
58   }
59
60   public int getLocalPort()
61   {
62     return impl.getLocalPort();
63   }
64
65   public Socket accept ()  throws IOException
66   {
67     Socket s = new Socket(Socket.factory == null ? new PlainSocketImpl()
68                           : Socket.factory.createSocketImpl());
69     implAccept (s);
70     return s;
71   }
72
73   protected final void implAccept (Socket s)  throws IOException
74   {
75     impl.accept(s.impl);
76   }
77
78   public void close () throws IOException
79   {
80     impl.close();
81   }
82
83   public synchronized void setSoTimeout (int timeout) throws SocketException
84   {
85     if (timeout < 0)
86       throw new IllegalArgumentException("Invalid timeout: " + timeout);
87
88     impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
89   }
90
91   public synchronized int getSoTimeout () throws SocketException
92   {
93     Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
94     if (timeout instanceof Integer) 
95       return ((Integer)timeout).intValue();
96     else
97       return 0;
98   }
99
100   public String toString ()
101   {
102     return "ServerSocket" + impl.toString();
103   }
104
105   public static synchronized void setSocketFactory (SocketImplFactory fac)
106     throws IOException
107   {
108     factory = fac;
109   }
110 }