]> gcc.gnu.org Git - gcc.git/blame - libjava/java/net/Socket.java
All files: Updated copyright information.
[gcc.git] / libjava / java / net / Socket.java
CommitLineData
ee9dd372
TT
1// Socket.java
2
2ba5f774 3/* Copyright (C) 1999 Free Software Foundation
ee9dd372
TT
4
5 This file is part of libgcj.
6
7This software is copyrighted work licensed under the terms of the
8Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9details. */
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.
f2ed9e96 17 * Status: I believe all methods are implemented.
ee9dd372
TT
18 */
19
20package java.net;
21import java.io.*;
22
23public 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);
07515641
WL
45 // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
46 // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
47 // that default. JDK 1.2 doc infers not to do a bind.
ee9dd372
TT
48 impl.connect(host, port);
49 }
50
51 public Socket (InetAddress address, int port)
52 throws IOException
53 {
54 this(factory == null ? new PlainSocketImpl() : factory.createSocketImpl());
55 SecurityManager s = System.getSecurityManager();
56 if (s != null)
57 s.checkConnect(address.getHostName(), port);
58 impl.create(true);
07515641
WL
59 // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
60 // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
61 // that default. JDK 1.2 doc infers not to do a bind.
ee9dd372
TT
62 impl.connect(address, port);
63 }
64
65 public Socket (String host, int port,
66 InetAddress localAddr, int localPort) throws IOException
67 {
68 this(factory == null ? new PlainSocketImpl() : factory.createSocketImpl());
69 SecurityManager s = System.getSecurityManager();
70 if (s != null)
71 s.checkConnect(host, port);
72 impl.create(true);
07515641 73 // FIXME: JCL p. 1587 says if localAddr is null, use getLocalAddress().
ee9dd372
TT
74 impl.bind(localAddr, localPort);
75 impl.connect(host, port);
76 }
77
78 public Socket (InetAddress address, int port,
79 InetAddress localAddr, int localPort) throws IOException
80 {
81 this(factory == null ? new PlainSocketImpl() : factory.createSocketImpl());
82 SecurityManager s = System.getSecurityManager();
83 if (s != null)
84 s.checkConnect(address.getHostName(), port);
85 impl.create(true);
07515641 86 // FIXME: JCL p. 1587 says if localAddr is null, use getLocalAddress().
ee9dd372
TT
87 impl.bind(localAddr, localPort);
88 impl.connect(address, port);
89 }
90
91 /**
92 * @deprecated Use DatagramSocket instead for UDP transport.
93 */
94 public Socket (String host, int port, boolean stream) throws IOException
95 {
96 impl = factory == null ? new PlainSocketImpl()
97 : factory.createSocketImpl();
98 impl.create(stream);
99 SecurityManager s = System.getSecurityManager();
100 if (s != null)
101 s.checkConnect(host, port);
07515641
WL
102 // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
103 // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
104 // that default. JDK 1.2 doc infers not to do a bind.
ee9dd372
TT
105 impl.connect(host, port);
106 }
107
108 /**
109 * @deprecated Use DatagramSocket instead for UDP transport.
110 */
111 public Socket (InetAddress host, int port, boolean stream) throws IOException
112 {
113 impl = factory == null ? new PlainSocketImpl()
114 : factory.createSocketImpl();
115 impl.create(stream);
116 SecurityManager s = System.getSecurityManager();
117 if (s != null)
118 s.checkConnect(host.getHostName(), port);
07515641
WL
119 // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
120 // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
121 // that default. JDK 1.2 doc infers not to do a bind.
ee9dd372
TT
122 impl.connect(host, port);
123 }
124
125 public InetAddress getInetAddress ()
126 {
127 return impl.getInetAddress();
128 }
129
130 public InetAddress getLocalAddress ()
131 {
1920afb3 132 // FIXME: see note in DatagramSocket.java about checkConnect() and security
f2ed9e96
WL
133 try
134 {
1920afb3 135 return (InetAddress)impl.getOption(SocketOptions.SO_BINDADDR);
f2ed9e96
WL
136 }
137 catch (SocketException x)
138 {
139 // (hopefully) shouldn't happen
140 System.err.println(x);
141 throw new java.lang.InternalError("Error in PlainSocketImpl.getOption");
142 }
ee9dd372
TT
143 }
144
145 public int getPort ()
146 {
147 return impl.getPort();
148 }
149
150 public int getLocalPort ()
151 {
152 return impl.getLocalPort();
153 }
154
155 public InputStream getInputStream () throws IOException
156 {
157 return impl.getInputStream();
158 }
159
160 public OutputStream getOutputStream () throws IOException
161 {
162 return impl.getOutputStream();
163 }
164
165 public void setTcpNoDelay (boolean on) throws SocketException
166 {
f2ed9e96 167 impl.setOption( SocketOptions.TCP_NODELAY, new Boolean(on) );
ee9dd372
TT
168 }
169
170 public boolean getTcpNoDelay() throws SocketException
171 {
f2ed9e96
WL
172 Boolean bool = (Boolean)impl.getOption( SocketOptions.TCP_NODELAY );
173 return bool.booleanValue();
ee9dd372
TT
174 }
175
176 public void setSoLinger(boolean on, int linger) throws SocketException
177 {
f2ed9e96
WL
178 if ( on && (linger >= 0) )
179 {
180 if (linger > 65535)
181 linger = 65535;
182 impl.setOption( SocketOptions.SO_LINGER, new Integer(linger) );
183 }
184 else if ( on && (linger < 0) )
185 throw new IllegalArgumentException("SO_LINGER must be >= 0");
186 else
187 impl.setOption( SocketOptions.SO_LINGER, new Boolean(false) );
ee9dd372
TT
188 }
189
12571b1f 190 public int getSoLinger() throws SocketException
ee9dd372 191 {
f2ed9e96
WL
192 Object linger = impl.getOption(SocketOptions.SO_LINGER);
193 if (linger instanceof Integer)
194 return ((Integer)linger).intValue();
195 else
196 return -1;
ee9dd372
TT
197 }
198
f2ed9e96 199 public synchronized void setSoTimeout (int timeout) throws SocketException
ee9dd372 200 {
f2ed9e96
WL
201 if (timeout < 0)
202 throw new IllegalArgumentException("Invalid timeout: " + timeout);
203
204 impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
ee9dd372
TT
205 }
206
f2ed9e96 207 public synchronized int getSoTimeout () throws SocketException
ee9dd372 208 {
f2ed9e96
WL
209 Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
210 if (timeout instanceof Integer)
211 return ((Integer)timeout).intValue();
212 else
213 return 0;
ee9dd372
TT
214 }
215
f2ed9e96 216 // JDK1.2
ee9dd372
TT
217 public void setSendBufferSize (int size) throws SocketException
218 {
f2ed9e96
WL
219 if (size <= 0)
220 throw new IllegalArgumentException("Invalid buffer size: " + size);
221
222 impl.setOption(SocketOptions.SO_SNDBUF, new Integer(size));
ee9dd372
TT
223 }
224
f2ed9e96 225 // JDK1.2
ee9dd372
TT
226 public int getSendBufferSize () throws SocketException
227 {
f2ed9e96
WL
228 Integer buf = (Integer)impl.getOption(SocketOptions.SO_SNDBUF);
229 return buf.intValue();
ee9dd372
TT
230 }
231
f2ed9e96 232 // JDK1.2
ee9dd372
TT
233 public void setReceiveBufferSize (int size) throws SocketException
234 {
f2ed9e96
WL
235 if (size <= 0)
236 throw new IllegalArgumentException("Invalid buffer size: " + size);
237
238 impl.setOption(SocketOptions.SO_RCVBUF, new Integer(size));
ee9dd372
TT
239 }
240
f2ed9e96 241 // JDK1.2
ee9dd372
TT
242 public int getReceiveBufferSize () throws SocketException
243 {
f2ed9e96
WL
244 Integer buf = (Integer)impl.getOption(SocketOptions.SO_RCVBUF);
245 return buf.intValue();
ee9dd372
TT
246 }
247
f2ed9e96 248 public synchronized void close () throws IOException
ee9dd372
TT
249 {
250 impl.close();
251 }
252
253 public String toString ()
254 {
f2ed9e96 255 return "Socket" + impl.toString();
ee9dd372
TT
256 }
257
f2ed9e96 258 public static synchronized void setSocketImplFactory (SocketImplFactory fac)
ee9dd372
TT
259 throws IOException
260 {
261 factory = fac;
262 }
263}
This page took 0.114393 seconds and 5 git commands to generate.