3 /* Copyright (C) 1999 Red Hat, Inc.
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 * @author Per Bothner <bothner@cygnus.com>
13 * @date January 6, 1999.
16 /** Written using on-line Java Platform 1.2 API Specification.
17 * Status: I believe all methods are implemented.
25 static SocketImplFactory factory
;
32 protected Socket (SocketImpl impl
) throws SocketException
37 public Socket (String host
, int port
)
38 throws UnknownHostException
, IOException
40 this(factory
== null ?
new PlainSocketImpl() : factory
.createSocketImpl());
41 SecurityManager s
= System
.getSecurityManager();
43 s
.checkConnect(host
, port
);
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.
48 impl
.connect(host
, port
);
51 public Socket (InetAddress address
, int port
)
54 this(factory
== null ?
new PlainSocketImpl() : factory
.createSocketImpl());
55 SecurityManager s
= System
.getSecurityManager();
57 s
.checkConnect(address
.getHostName(), port
);
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.
62 impl
.connect(address
, port
);
65 public Socket (String host
, int port
,
66 InetAddress localAddr
, int localPort
) throws IOException
68 this(factory
== null ?
new PlainSocketImpl() : factory
.createSocketImpl());
69 SecurityManager s
= System
.getSecurityManager();
71 s
.checkConnect(host
, port
);
73 // FIXME: JCL p. 1587 says if localAddr is null, use getLocalAddress().
74 impl
.bind(localAddr
, localPort
);
75 impl
.connect(host
, port
);
78 public Socket (InetAddress address
, int port
,
79 InetAddress localAddr
, int localPort
) throws IOException
81 this(factory
== null ?
new PlainSocketImpl() : factory
.createSocketImpl());
82 SecurityManager s
= System
.getSecurityManager();
84 s
.checkConnect(address
.getHostName(), port
);
86 // FIXME: JCL p. 1587 says if localAddr is null, use getLocalAddress().
87 impl
.bind(localAddr
, localPort
);
88 impl
.connect(address
, port
);
92 * @deprecated Use DatagramSocket instead for UDP transport.
94 public Socket (String host
, int port
, boolean stream
) throws IOException
96 impl
= factory
== null ?
new PlainSocketImpl()
97 : factory
.createSocketImpl();
99 SecurityManager s
= System
.getSecurityManager();
101 s
.checkConnect(host
, port
);
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.
105 impl
.connect(host
, port
);
109 * @deprecated Use DatagramSocket instead for UDP transport.
111 public Socket (InetAddress host
, int port
, boolean stream
) throws IOException
113 impl
= factory
== null ?
new PlainSocketImpl()
114 : factory
.createSocketImpl();
116 SecurityManager s
= System
.getSecurityManager();
118 s
.checkConnect(host
.getHostName(), port
);
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.
122 impl
.connect(host
, port
);
125 public InetAddress
getInetAddress ()
127 return impl
.getInetAddress();
130 public InetAddress
getLocalAddress ()
132 // FIXME: see note in DatagramSocket.java about checkConnect() and security
135 return (InetAddress
)impl
.getOption(SocketOptions
.SO_BINDADDR
);
137 catch (SocketException x
)
139 // (hopefully) shouldn't happen
140 System
.err
.println(x
);
141 throw new java
.lang
.InternalError("Error in PlainSocketImpl.getOption");
145 public int getPort ()
147 return impl
.getPort();
150 public int getLocalPort ()
152 return impl
.getLocalPort();
155 public InputStream
getInputStream () throws IOException
157 return impl
.getInputStream();
160 public OutputStream
getOutputStream () throws IOException
162 return impl
.getOutputStream();
165 public void setTcpNoDelay (boolean on
) throws SocketException
167 impl
.setOption( SocketOptions
.TCP_NODELAY
, new Boolean(on
) );
170 public boolean getTcpNoDelay() throws SocketException
172 Boolean bool
= (Boolean
)impl
.getOption( SocketOptions
.TCP_NODELAY
);
173 return bool
.booleanValue();
176 public void setSoLinger(boolean on
, int linger
) throws SocketException
178 if ( on
&& (linger
>= 0) )
182 impl
.setOption( SocketOptions
.SO_LINGER
, new Integer(linger
) );
184 else if ( on
&& (linger
< 0) )
185 throw new IllegalArgumentException("SO_LINGER must be >= 0");
187 impl
.setOption( SocketOptions
.SO_LINGER
, new Boolean(false) );
190 public int getSoLinger() throws SocketException
192 Object linger
= impl
.getOption(SocketOptions
.SO_LINGER
);
193 if (linger
instanceof Integer
)
194 return ((Integer
)linger
).intValue();
199 public synchronized void setSoTimeout (int timeout
) throws SocketException
202 throw new IllegalArgumentException("Invalid timeout: " + timeout
);
204 impl
.setOption(SocketOptions
.SO_TIMEOUT
, new Integer(timeout
));
207 public synchronized int getSoTimeout () throws SocketException
209 Object timeout
= impl
.getOption(SocketOptions
.SO_TIMEOUT
);
210 if (timeout
instanceof Integer
)
211 return ((Integer
)timeout
).intValue();
217 public void setSendBufferSize (int size
) throws SocketException
220 throw new IllegalArgumentException("Invalid buffer size: " + size
);
222 impl
.setOption(SocketOptions
.SO_SNDBUF
, new Integer(size
));
226 public int getSendBufferSize () throws SocketException
228 Integer buf
= (Integer
)impl
.getOption(SocketOptions
.SO_SNDBUF
);
229 return buf
.intValue();
233 public void setReceiveBufferSize (int size
) throws SocketException
236 throw new IllegalArgumentException("Invalid buffer size: " + size
);
238 impl
.setOption(SocketOptions
.SO_RCVBUF
, new Integer(size
));
242 public int getReceiveBufferSize () throws SocketException
244 Integer buf
= (Integer
)impl
.getOption(SocketOptions
.SO_RCVBUF
);
245 return buf
.intValue();
248 public synchronized void close () throws IOException
253 public String
toString ()
255 return "Socket" + impl
.toString();
258 public static synchronized void setSocketImplFactory (SocketImplFactory fac
)