[multiple changes]
[gcc.git] / libjava / java / net / Socket.java
1 // Socket.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 Socket
24 {
25 static SocketImplFactory factory;
26 SocketImpl impl;
27
28 protected Socket ()
29 {
30 }
31
32 protected Socket (SocketImpl impl) throws SocketException
33 {
34 this.impl = impl;
35 }
36
37 public Socket (String host, int port)
38 throws UnknownHostException, IOException
39 {
40 this(factory == null ? new PlainSocketImpl() : factory.createSocketImpl());
41 SecurityManager s = System.getSecurityManager();
42 if (s != null)
43 s.checkConnect(host, port);
44 impl.create(true);
45 impl.connect(host, port);
46 }
47
48 public Socket (InetAddress address, int port)
49 throws IOException
50 {
51 this(factory == null ? new PlainSocketImpl() : factory.createSocketImpl());
52 SecurityManager s = System.getSecurityManager();
53 if (s != null)
54 s.checkConnect(address.getHostName(), port);
55 impl.create(true);
56 impl.connect(address, port);
57 }
58
59 public Socket (String host, int port,
60 InetAddress localAddr, int localPort) throws IOException
61 {
62 this(factory == null ? new PlainSocketImpl() : factory.createSocketImpl());
63 SecurityManager s = System.getSecurityManager();
64 if (s != null)
65 s.checkConnect(host, port);
66 impl.create(true);
67 impl.bind(localAddr, localPort);
68 impl.connect(host, port);
69 }
70
71 public Socket (InetAddress address, int port,
72 InetAddress localAddr, int localPort) throws IOException
73 {
74 this(factory == null ? new PlainSocketImpl() : factory.createSocketImpl());
75 SecurityManager s = System.getSecurityManager();
76 if (s != null)
77 s.checkConnect(address.getHostName(), port);
78 impl.create(true);
79 impl.bind(localAddr, localPort);
80 impl.connect(address, port);
81 }
82
83 /**
84 * @deprecated Use DatagramSocket instead for UDP transport.
85 */
86 public Socket (String host, int port, boolean stream) throws IOException
87 {
88 impl = factory == null ? new PlainSocketImpl()
89 : factory.createSocketImpl();
90 impl.create(stream);
91 SecurityManager s = System.getSecurityManager();
92 if (s != null)
93 s.checkConnect(host, port);
94 impl.connect(host, port);
95 }
96
97 /**
98 * @deprecated Use DatagramSocket instead for UDP transport.
99 */
100 public Socket (InetAddress host, int port, boolean stream) throws IOException
101 {
102 impl = factory == null ? new PlainSocketImpl()
103 : factory.createSocketImpl();
104 impl.create(stream);
105 SecurityManager s = System.getSecurityManager();
106 if (s != null)
107 s.checkConnect(host.getHostName(), port);
108 impl.connect(host, port);
109 }
110
111 public InetAddress getInetAddress ()
112 {
113 return impl.getInetAddress();
114 }
115
116 public InetAddress getLocalAddress ()
117 {
118 InetAddress localAddress;
119 try
120 {
121 localAddress = (InetAddress)impl.getOption(SocketOptions.SO_BINDADDR);
122 }
123 catch (SocketException x)
124 {
125 // (hopefully) shouldn't happen
126 System.err.println(x);
127 throw new java.lang.InternalError("Error in PlainSocketImpl.getOption");
128 }
129 return localAddress;
130 }
131
132 public int getPort ()
133 {
134 return impl.getPort();
135 }
136
137 public int getLocalPort ()
138 {
139 return impl.getLocalPort();
140 }
141
142 public InputStream getInputStream () throws IOException
143 {
144 return impl.getInputStream();
145 }
146
147 public OutputStream getOutputStream () throws IOException
148 {
149 return impl.getOutputStream();
150 }
151
152 public void setTcpNoDelay (boolean on) throws SocketException
153 {
154 impl.setOption( SocketOptions.TCP_NODELAY, new Boolean(on) );
155 }
156
157 public boolean getTcpNoDelay() throws SocketException
158 {
159 Boolean bool = (Boolean)impl.getOption( SocketOptions.TCP_NODELAY );
160 return bool.booleanValue();
161 }
162
163 public void setSoLinger(boolean on, int linger) throws SocketException
164 {
165 if ( on && (linger >= 0) )
166 {
167 if (linger > 65535)
168 linger = 65535;
169 impl.setOption( SocketOptions.SO_LINGER, new Integer(linger) );
170 }
171 else if ( on && (linger < 0) )
172 throw new IllegalArgumentException("SO_LINGER must be >= 0");
173 else
174 impl.setOption( SocketOptions.SO_LINGER, new Boolean(false) );
175 }
176
177 public int getSoLinger() throws SocketException
178 {
179 Object linger = impl.getOption(SocketOptions.SO_LINGER);
180 if (linger instanceof Integer)
181 return ((Integer)linger).intValue();
182 else
183 return -1;
184 }
185
186 public synchronized void setSoTimeout (int timeout) throws SocketException
187 {
188 if (timeout < 0)
189 throw new IllegalArgumentException("Invalid timeout: " + timeout);
190
191 impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
192 }
193
194 public synchronized int getSoTimeout () throws SocketException
195 {
196 Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
197 if (timeout instanceof Integer)
198 return ((Integer)timeout).intValue();
199 else
200 return 0;
201 }
202
203 // JDK1.2
204 public void setSendBufferSize (int size) throws SocketException
205 {
206 if (size <= 0)
207 throw new IllegalArgumentException("Invalid buffer size: " + size);
208
209 impl.setOption(SocketOptions.SO_SNDBUF, new Integer(size));
210 }
211
212 // JDK1.2
213 public int getSendBufferSize () throws SocketException
214 {
215 Integer buf = (Integer)impl.getOption(SocketOptions.SO_SNDBUF);
216 return buf.intValue();
217 }
218
219 // JDK1.2
220 public void setReceiveBufferSize (int size) throws SocketException
221 {
222 if (size <= 0)
223 throw new IllegalArgumentException("Invalid buffer size: " + size);
224
225 impl.setOption(SocketOptions.SO_RCVBUF, new Integer(size));
226 }
227
228 // JDK1.2
229 public int getReceiveBufferSize () throws SocketException
230 {
231 Integer buf = (Integer)impl.getOption(SocketOptions.SO_RCVBUF);
232 return buf.intValue();
233 }
234
235 public synchronized void close () throws IOException
236 {
237 impl.close();
238 }
239
240 public String toString ()
241 {
242 return "Socket" + impl.toString();
243 }
244
245 public static synchronized void setSocketImplFactory (SocketImplFactory fac)
246 throws IOException
247 {
248 factory = fac;
249 }
250 }