OSDN Git Service

* All files: Updated copyright information.
[pf3gnuchains/gcc-fork.git] / libjava / java / net / ServerSocket.java
1 // ServerSocket.java
2
3 /* Copyright (C) 1999  Free Software Foundation
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   static final byte[] zeros = {0,0,0,0};
29   /* dummy InetAddress, used to bind socket to any (all) network interfaces */
30   static final InetAddress ANY_IF = new InetAddress(zeros, null);
31
32   public ServerSocket (int port)
33     throws java.io.IOException
34   {
35     this(port, 50);
36   }
37
38   public ServerSocket (int port, int backlog)
39     throws java.io.IOException
40   {
41     this(port, backlog, ANY_IF);
42   }
43
44   public ServerSocket (int port, int backlog, InetAddress bindAddr)
45     throws java.io.IOException
46   {
47     if (factory == null)
48       this.impl = new PlainSocketImpl();
49     else
50       this.impl = factory.createSocketImpl();
51     SecurityManager s = System.getSecurityManager();
52     if (s != null)
53       s.checkListen(port);
54     impl.create(true);
55     impl.bind(bindAddr == null ? ANY_IF : bindAddr, port);
56     impl.listen(backlog);
57   }
58
59   public InetAddress getInetAddress()
60   {
61     return impl.getInetAddress();
62   }
63
64   public int getLocalPort()
65   {
66     return impl.getLocalPort();
67   }
68
69   public Socket accept ()  throws IOException
70   {
71     Socket s = new Socket(Socket.factory == null ? new PlainSocketImpl()
72                           : Socket.factory.createSocketImpl());
73     implAccept (s);
74     return s;
75   }
76
77   protected final void implAccept (Socket s)  throws IOException
78   {
79     impl.accept(s.impl);
80   }
81
82   public void close () throws IOException
83   {
84     impl.close();
85   }
86
87   public synchronized void setSoTimeout (int timeout) throws SocketException
88   {
89     if (timeout < 0)
90       throw new IllegalArgumentException("Invalid timeout: " + timeout);
91
92     impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
93   }
94
95   public synchronized int getSoTimeout () throws SocketException
96   {
97     Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
98     if (timeout instanceof Integer) 
99       return ((Integer)timeout).intValue();
100     else
101       return 0;
102   }
103
104   public String toString ()
105   {
106     return "ServerSocket" + impl.toString();
107   }
108
109   public static synchronized void setSocketFactory (SocketImplFactory fac)
110     throws IOException
111   {
112     factory = fac;
113   }
114 }