1 /* Socket.java -- Client socket implementation
2 Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
41 import gnu
.java
.net
.PlainSocketImpl
;
42 import java
.io
.IOException
;
43 import java
.io
.InputStream
;
44 import java
.io
.OutputStream
;
45 import java
.nio
.channels
.IllegalBlockingModeException
;
46 import java
.nio
.channels
.SocketChannel
;
49 /* Written using on-line Java Platform 1.2 API Specification.
50 * Status: I believe all methods are implemented.
54 * This class models a client site socket. A socket is a TCP/IP endpoint
55 * for network communications conceptually similar to a file handle.
57 * This class does not actually do any work. Instead, it redirects all of
58 * its calls to a socket implementation object which implements the
59 * <code>SocketImpl</code> interface. The implementation class is
60 * instantiated by factory class that implements the
61 * <code>SocketImplFactory interface</code>. A default
62 * factory is provided, however the factory may be set by a call to
63 * the <code>setSocketImplFactory</code> method. Note that this may only be
64 * done once per virtual machine. If a subsequent attempt is made to set the
65 * factory, a <code>SocketException</code> will be thrown.
67 * @author Aaron M. Renn (arenn@urbanophile.com)
68 * @author Per Bothner (bothner@cygnus.com)
73 * This is the user SocketImplFactory for this class. If this variable is
74 * null, a default factory is used.
76 static SocketImplFactory factory
;
79 * The implementation object to which calls are redirected
81 // package-private because ServerSocket.implAccept() needs to access it.
85 * True if socket implementation was created by calling their
88 // package-private because ServerSocket.implAccept() needs to access it.
92 * True if the socket is bound.
94 private boolean bound
;
97 * True if input is shutdown.
99 private boolean inputShutdown
;
102 * True if output is shutdown.
104 private boolean outputShutdown
;
107 * Initializes a new instance of <code>Socket</code> object without
108 * connecting to a remote host. This useful for subclasses of socket that
109 * might want this behavior.
111 * @specnote This constructor is public since JDK 1.4
117 impl
= factory
.createSocketImpl();
119 impl
= new PlainSocketImpl();
123 * Initializes a new instance of <code>Socket</code> object without
124 * connecting to a remote host. This is useful for subclasses of socket
125 * that might want this behavior.
127 * Additionally, this socket will be created using the supplied
128 * implementation class instead the default class or one returned by a
129 * factory. If this value is <code>null</code>, the default Socket
130 * implementation is used.
132 * @param impl The <code>SocketImpl</code> to use for this
133 * <code>Socket</code>
135 * @exception SocketException If an error occurs
139 protected Socket(SocketImpl impl
) throws SocketException
142 this.impl
= new PlainSocketImpl();
148 * Initializes a new instance of <code>Socket</code> and connects to the
149 * hostname and port specified as arguments.
151 * @param host The name of the host to connect to
152 * @param port The port number to connect to
154 * @exception UnknownHostException If the hostname cannot be resolved to a
156 * @exception IOException If an error occurs
157 * @exception SecurityException If a security manager exists and its
158 * checkConnect method doesn't allow the operation
160 public Socket(String host
, int port
)
161 throws UnknownHostException
, IOException
163 this(InetAddress
.getByName(host
), port
, null, 0, true);
167 * Initializes a new instance of <code>Socket</code> and connects to the
168 * address and port number specified as arguments.
170 * @param address The address to connect to
171 * @param port The port number to connect to
173 * @exception IOException If an error occurs
174 * @exception SecurityException If a security manager exists and its
175 * checkConnect method doesn't allow the operation
177 public Socket(InetAddress address
, int port
) throws IOException
179 this(address
, port
, null, 0, true);
183 * Initializes a new instance of <code>Socket</code> that connects to the
184 * named host on the specified port and binds to the specified local address
187 * @param host The name of the remote host to connect to.
188 * @param port The remote port to connect to.
189 * @param localAddr The local address to bind to.
190 * @param localPort The local port to bind to.
192 * @exception SecurityException If the <code>SecurityManager</code>
193 * exists and does not allow a connection to the specified host/port or
194 * binding to the specified local host/port.
195 * @exception IOException If a connection error occurs.
199 public Socket(String host
, int port
, InetAddress localAddr
, int localPort
)
202 this(InetAddress
.getByName(host
), port
, localAddr
, localPort
, true);
206 * Initializes a new instance of <code>Socket</code> and connects to the
207 * address and port number specified as arguments, plus binds to the
208 * specified local address and port.
210 * @param address The remote address to connect to
211 * @param port The remote port to connect to
212 * @param localAddr The local address to connect to
213 * @param localPort The local port to connect to
215 * @exception IOException If an error occurs
216 * @exception SecurityException If a security manager exists and its
217 * checkConnect method doesn't allow the operation
221 public Socket(InetAddress address
, int port
, InetAddress localAddr
,
222 int localPort
) throws IOException
224 this(address
, port
, localAddr
, localPort
, true);
228 * Initializes a new instance of <code>Socket</code> and connects to the
229 * hostname and port specified as arguments. If the stream argument is set
230 * to <code>true</code>, then a stream socket is created. If it is
231 * <code>false</code>, a datagram socket is created.
233 * @param host The name of the host to connect to
234 * @param port The port to connect to
235 * @param stream <code>true</code> for a stream socket, <code>false</code>
236 * for a datagram socket
238 * @exception IOException If an error occurs
239 * @exception SecurityException If a security manager exists and its
240 * checkConnect method doesn't allow the operation
242 * @deprecated Use the <code>DatagramSocket</code> class to create
243 * datagram oriented sockets.
245 public Socket(String host
, int port
, boolean stream
)
248 this(InetAddress
.getByName(host
), port
, null, 0, stream
);
252 * Initializes a new instance of <code>Socket</code> and connects to the
253 * address and port number specified as arguments. If the stream param is
254 * <code>true</code>, a stream socket will be created, otherwise a datagram
257 * @param host The address to connect to
258 * @param port The port number to connect to
259 * @param stream <code>true</code> to create a stream socket,
260 * <code>false</code> to create a datagram socket.
262 * @exception IOException If an error occurs
263 * @exception SecurityException If a security manager exists and its
264 * checkConnect method doesn't allow the operation
266 * @deprecated Use the <code>DatagramSocket</code> class to create
267 * datagram oriented sockets.
269 public Socket(InetAddress host
, int port
, boolean stream
)
272 this(host
, port
, null, 0, stream
);
276 * This constructor is where the real work takes place. Connect to the
277 * specified address and port. Use default local values if not specified,
278 * otherwise use the local host and port passed in. Create as stream or
279 * datagram based on "stream" argument.
282 * @param raddr The remote address to connect to
283 * @param rport The remote port to connect to
284 * @param laddr The local address to connect to
285 * @param lport The local port to connect to
286 * @param stream true for a stream socket, false for a datagram socket
288 * @exception IOException If an error occurs
289 * @exception SecurityException If a security manager exists and its
290 * checkConnect method doesn't allow the operation
292 private Socket(InetAddress raddr
, int rport
, InetAddress laddr
, int lport
,
293 boolean stream
) throws IOException
297 SecurityManager sm
= System
.getSecurityManager();
299 sm
.checkConnect(raddr
.getHostName(), rport
);
302 SocketAddress bindaddr
=
303 laddr
== null ?
null : new InetSocketAddress(laddr
, lport
);
307 connect(new InetSocketAddress(raddr
, rport
));
309 // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
310 // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
311 // that default. JDK 1.2 doc infers not to do a bind.
314 // This has to be accessible from java.net.ServerSocket.
315 SocketImpl
getImpl() throws SocketException
325 catch (IOException e
)
327 throw new SocketException(e
.getMessage());
334 * Binds the socket to the givent local address/port
336 * @param bindpoint The address/port to bind to
338 * @exception IOException If an error occurs
339 * @exception SecurityException If a security manager exists and its
340 * checkConnect method doesn't allow the operation
341 * @exception IllegalArgumentException If the address type is not supported
345 public void bind(SocketAddress bindpoint
) throws IOException
348 throw new SocketException("socket is closed");
350 // XXX: JDK 1.4.1 API documentation says that if bindpoint is null the
351 // socket will be bound to an ephemeral port and a valid local address.
352 if (bindpoint
== null)
353 bindpoint
= new InetSocketAddress(InetAddress
.ANY_IF
, 0);
355 if (! (bindpoint
instanceof InetSocketAddress
))
356 throw new IllegalArgumentException();
358 InetSocketAddress tmp
= (InetSocketAddress
) bindpoint
;
360 // bind to address/port
363 getImpl().bind(tmp
.getAddress(), tmp
.getPort());
366 catch (IOException exception
)
371 catch (RuntimeException exception
)
384 * Connects the socket with a remote address.
386 * @param endpoint The address to connect to
388 * @exception IOException If an error occurs
389 * @exception IllegalArgumentException If the addess type is not supported
390 * @exception IllegalBlockingModeException If this socket has an associated
391 * channel, and the channel is in non-blocking mode
395 public void connect(SocketAddress endpoint
) throws IOException
397 connect(endpoint
, 0);
401 * Connects the socket with a remote address. A timeout of zero is
402 * interpreted as an infinite timeout. The connection will then block
403 * until established or an error occurs.
405 * @param endpoint The address to connect to
406 * @param timeout The length of the timeout in milliseconds, or
407 * 0 to indicate no timeout.
409 * @exception IOException If an error occurs
410 * @exception IllegalArgumentException If the address type is not supported
411 * @exception IllegalBlockingModeException If this socket has an associated
412 * channel, and the channel is in non-blocking mode
413 * @exception SocketTimeoutException If the timeout is reached
417 public void connect(SocketAddress endpoint
, int timeout
)
421 throw new SocketException("socket is closed");
423 if (! (endpoint
instanceof InetSocketAddress
))
424 throw new IllegalArgumentException("unsupported address type");
426 // The Sun spec says that if we have an associated channel and
427 // it is in non-blocking mode, we throw an IllegalBlockingModeException.
428 // However, in our implementation if the channel itself initiated this
429 // operation, then we must honor it regardless of its blocking mode.
430 if (getChannel() != null && ! getChannel().isBlocking()
431 && ! ((PlainSocketImpl
) getImpl()).isInChannelOperation())
432 throw new IllegalBlockingModeException();
439 getImpl().connect(endpoint
, timeout
);
441 catch (IOException exception
)
446 catch (RuntimeException exception
)
459 * Returns the address of the remote end of the socket. If this socket
460 * is not connected, then <code>null</code> is returned.
462 * @return The remote address this socket is connected to
464 public InetAddress
getInetAddress()
471 return getImpl().getInetAddress();
473 catch (SocketException e
)
475 // This cannot happen as we are connected.
482 * Returns the local address to which this socket is bound. If this socket
483 * is not connected, then <code>null</code> is returned.
485 * @return The local address
489 public InetAddress
getLocalAddress()
491 InetAddress addr
= null;
495 addr
= (InetAddress
) getImpl().getOption(SocketOptions
.SO_BINDADDR
);
497 catch (SocketException e
)
499 // (hopefully) shouldn't happen
500 // throw new java.lang.InternalError
501 // ("Error in PlainSocketImpl.getOption");
505 // FIXME: According to libgcj, checkConnect() is supposed to be called
506 // before performing this operation. Problems: 1) We don't have the
507 // addr until after we do it, so we do a post check. 2). The docs I
508 // see don't require this in the Socket case, only DatagramSocket, but
509 // we'll assume they mean both.
510 SecurityManager sm
= System
.getSecurityManager();
512 sm
.checkConnect(addr
.getHostName(), getLocalPort());
518 * Returns the port number of the remote end of the socket connection. If
519 * this socket is not connected, then -1 is returned.
521 * @return The remote port this socket is connected to
530 if (getImpl() != null)
531 return getImpl().getPort();
533 catch (SocketException e
)
535 // This cannot happen as we are connected.
542 * Returns the local port number to which this socket is bound. If this
543 * socket is not connected, then -1 is returned.
545 * @return The local port
547 public int getLocalPort()
554 if (getImpl() != null)
555 return getImpl().getLocalPort();
557 catch (SocketException e
)
559 // This cannot happen as we are bound.
566 * Returns local socket address.
568 * @return the local socket address, null if not bound
572 public SocketAddress
getLocalSocketAddress()
577 InetAddress addr
= getLocalAddress();
581 return new InetSocketAddress(addr
, getImpl().getLocalPort());
583 catch (SocketException e
)
585 // This cannot happen as we are bound.
591 * Returns the remote socket address.
593 * @return the remote socket address, null of not connected
597 public SocketAddress
getRemoteSocketAddress()
604 return new InetSocketAddress(getImpl().getInetAddress(),
605 getImpl().getPort());
607 catch (SocketException e
)
609 // This cannot happen as we are connected.
615 * Returns an InputStream for reading from this socket.
617 * @return The InputStream object
619 * @exception IOException If an error occurs or Socket is not connected
621 public InputStream
getInputStream() throws IOException
624 throw new SocketException("socket is closed");
627 throw new IOException("not connected");
629 return getImpl().getInputStream();
633 * Returns an OutputStream for writing to this socket.
635 * @return The OutputStream object
637 * @exception IOException If an error occurs or Socket is not connected
639 public OutputStream
getOutputStream() throws IOException
642 throw new SocketException("socket is closed");
645 throw new IOException("not connected");
647 return getImpl().getOutputStream();
651 * Sets the TCP_NODELAY option on the socket.
653 * @param on true to enable, false to disable
655 * @exception SocketException If an error occurs or Socket is not connected
659 public void setTcpNoDelay(boolean on
) throws SocketException
662 throw new SocketException("socket is closed");
664 getImpl().setOption(SocketOptions
.TCP_NODELAY
, Boolean
.valueOf(on
));
668 * Tests whether or not the TCP_NODELAY option is set on the socket.
669 * Returns true if enabled, false if disabled. When on it disables the
670 * Nagle algorithm which means that packets are always send immediatly and
671 * never merged together to reduce network trafic.
673 * @return Whether or not TCP_NODELAY is set
675 * @exception SocketException If an error occurs or Socket not connected
679 public boolean getTcpNoDelay() throws SocketException
682 throw new SocketException("socket is closed");
684 Object on
= getImpl().getOption(SocketOptions
.TCP_NODELAY
);
686 if (on
instanceof Boolean
)
687 return (((Boolean
) on
).booleanValue());
689 throw new SocketException("Internal Error");
693 * Sets the value of the SO_LINGER option on the socket. If the
694 * SO_LINGER option is set on a socket and there is still data waiting to
695 * be sent when the socket is closed, then the close operation will block
696 * until either that data is delivered or until the timeout period
697 * expires. The linger interval is specified in hundreths of a second
698 * (platform specific?)
700 * @param on true to enable SO_LINGER, false to disable
701 * @param linger The SO_LINGER timeout in hundreths of a second or -1 if
704 * @exception SocketException If an error occurs or Socket not connected
705 * @exception IllegalArgumentException If linger is negative
709 public void setSoLinger(boolean on
, int linger
) throws SocketException
712 throw new SocketException("socket is closed");
717 throw new IllegalArgumentException("SO_LINGER must be >= 0");
722 getImpl().setOption(SocketOptions
.SO_LINGER
, new Integer(linger
));
725 getImpl().setOption(SocketOptions
.SO_LINGER
, Boolean
.valueOf(false));
729 * Returns the value of the SO_LINGER option on the socket. If the
730 * SO_LINGER option is set on a socket and there is still data waiting to
731 * be sent when the socket is closed, then the close operation will block
732 * until either that data is delivered or until the timeout period
733 * expires. This method either returns the timeouts (in hundredths of
734 * of a second (platform specific?)) if SO_LINGER is set, or -1 if
735 * SO_LINGER is not set.
737 * @return The SO_LINGER timeout in hundreths of a second or -1
738 * if SO_LINGER not set
740 * @exception SocketException If an error occurs or Socket is not connected
744 public int getSoLinger() throws SocketException
747 throw new SocketException("socket is closed");
749 Object linger
= getImpl().getOption(SocketOptions
.SO_LINGER
);
751 if (linger
instanceof Integer
)
752 return (((Integer
) linger
).intValue());
758 * Sends urgent data through the socket
760 * @param data The data to send.
761 * Only the lowest eight bits of data are sent
763 * @exception IOException If an error occurs
767 public void sendUrgentData(int data
) throws IOException
770 throw new SocketException("socket is closed");
772 getImpl().sendUrgentData(data
);
776 * Enables/disables the SO_OOBINLINE option
778 * @param on True if SO_OOBLINE should be enabled
780 * @exception SocketException If an error occurs
784 public void setOOBInline(boolean on
) throws SocketException
787 throw new SocketException("socket is closed");
789 getImpl().setOption(SocketOptions
.SO_OOBINLINE
, Boolean
.valueOf(on
));
793 * Returns the current setting of the SO_OOBINLINE option for this socket
795 * @return True if SO_OOBINLINE is set, false otherwise.
797 * @exception SocketException If an error occurs
801 public boolean getOOBInline() throws SocketException
804 throw new SocketException("socket is closed");
806 Object buf
= getImpl().getOption(SocketOptions
.SO_OOBINLINE
);
808 if (buf
instanceof Boolean
)
809 return (((Boolean
) buf
).booleanValue());
811 throw new SocketException("Internal Error: Unexpected type");
815 * Sets the value of the SO_TIMEOUT option on the socket. If this value
816 * is set, and an read/write is performed that does not complete within
817 * the timeout period, a short count is returned (or an EWOULDBLOCK signal
818 * would be sent in Unix if no data had been read). A value of 0 for
819 * this option implies that there is no timeout (ie, operations will
820 * block forever). On systems that have separate read and write timeout
821 * values, this method returns the read timeout. This
822 * value is in milliseconds.
824 * @param timeout The length of the timeout in milliseconds, or
825 * 0 to indicate no timeout.
827 * @exception SocketException If an error occurs or Socket not connected
831 public synchronized void setSoTimeout(int timeout
) throws SocketException
834 throw new SocketException("socket is closed");
837 throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
839 getImpl().setOption(SocketOptions
.SO_TIMEOUT
, new Integer(timeout
));
843 * Returns the value of the SO_TIMEOUT option on the socket. If this value
844 * is set, and an read/write is performed that does not complete within
845 * the timeout period, a short count is returned (or an EWOULDBLOCK signal
846 * would be sent in Unix if no data had been read). A value of 0 for
847 * this option implies that there is no timeout (ie, operations will
848 * block forever). On systems that have separate read and write timeout
849 * values, this method returns the read timeout. This
850 * value is in thousandths of a second (implementation specific?).
852 * @return The length of the timeout in thousandth's of a second or 0
855 * @exception SocketException If an error occurs or Socket not connected
859 public synchronized int getSoTimeout() throws SocketException
862 throw new SocketException("socket is closed");
864 Object timeout
= getImpl().getOption(SocketOptions
.SO_TIMEOUT
);
865 if (timeout
instanceof Integer
)
866 return (((Integer
) timeout
).intValue());
872 * This method sets the value for the system level socket option
873 * SO_SNDBUF to the specified value. Note that valid values for this
874 * option are specific to a given operating system.
876 * @param size The new send buffer size.
878 * @exception SocketException If an error occurs or Socket not connected
879 * @exception IllegalArgumentException If size is 0 or negative
883 public void setSendBufferSize(int size
) throws SocketException
886 throw new SocketException("socket is closed");
889 throw new IllegalArgumentException("SO_SNDBUF value must be > 0");
891 getImpl().setOption(SocketOptions
.SO_SNDBUF
, new Integer(size
));
895 * This method returns the value of the system level socket option
896 * SO_SNDBUF, which is used by the operating system to tune buffer
897 * sizes for data transfers.
899 * @return The send buffer size.
901 * @exception SocketException If an error occurs or socket not connected
905 public int getSendBufferSize() throws SocketException
908 throw new SocketException("socket is closed");
910 Object buf
= getImpl().getOption(SocketOptions
.SO_SNDBUF
);
912 if (buf
instanceof Integer
)
913 return (((Integer
) buf
).intValue());
915 throw new SocketException("Internal Error: Unexpected type");
919 * This method sets the value for the system level socket option
920 * SO_RCVBUF to the specified value. Note that valid values for this
921 * option are specific to a given operating system.
923 * @param size The new receive buffer size.
925 * @exception SocketException If an error occurs or Socket is not connected
926 * @exception IllegalArgumentException If size is 0 or negative
930 public void setReceiveBufferSize(int size
) throws SocketException
933 throw new SocketException("socket is closed");
936 throw new IllegalArgumentException("SO_RCVBUF value must be > 0");
938 getImpl().setOption(SocketOptions
.SO_RCVBUF
, new Integer(size
));
942 * This method returns the value of the system level socket option
943 * SO_RCVBUF, which is used by the operating system to tune buffer
944 * sizes for data transfers.
946 * @return The receive buffer size.
948 * @exception SocketException If an error occurs or Socket is not connected
952 public int getReceiveBufferSize() throws SocketException
955 throw new SocketException("socket is closed");
957 Object buf
= getImpl().getOption(SocketOptions
.SO_RCVBUF
);
959 if (buf
instanceof Integer
)
960 return (((Integer
) buf
).intValue());
962 throw new SocketException("Internal Error: Unexpected type");
966 * This method sets the value for the socket level socket option
969 * @param on True if SO_KEEPALIVE should be enabled
971 * @exception SocketException If an error occurs or Socket is not connected
975 public void setKeepAlive(boolean on
) throws SocketException
978 throw new SocketException("socket is closed");
980 getImpl().setOption(SocketOptions
.SO_KEEPALIVE
, Boolean
.valueOf(on
));
984 * This method returns the value of the socket level socket option
987 * @return The setting
989 * @exception SocketException If an error occurs or Socket is not connected
993 public boolean getKeepAlive() throws SocketException
996 throw new SocketException("socket is closed");
998 Object buf
= getImpl().getOption(SocketOptions
.SO_KEEPALIVE
);
1000 if (buf
instanceof Boolean
)
1001 return (((Boolean
) buf
).booleanValue());
1003 throw new SocketException("Internal Error: Unexpected type");
1007 * Closes the socket.
1009 * @exception IOException If an error occurs
1011 public synchronized void close() throws IOException
1020 if (getChannel() != null)
1021 getChannel().close();
1025 * Converts this <code>Socket</code> to a <code>String</code>.
1027 * @return The <code>String</code> representation of this <code>Socket</code>
1029 public String
toString()
1034 return ("Socket[addr=" + getImpl().getInetAddress() + ",port="
1035 + getImpl().getPort() + ",localport="
1036 + getImpl().getLocalPort() + "]");
1038 catch (SocketException e
)
1040 // This cannot happen as we are connected.
1043 return "Socket[unconnected]";
1047 * Sets the <code>SocketImplFactory</code>. This may be done only once per
1048 * virtual machine. Subsequent attempts will generate a
1049 * <code>SocketException</code>. Note that a <code>SecurityManager</code>
1050 * check is made prior to setting the factory. If
1051 * insufficient privileges exist to set the factory, then an
1052 * <code>IOException</code> will be thrown.
1054 * @param fac the factory to set
1056 * @exception SecurityException If the <code>SecurityManager</code> does
1057 * not allow this operation.
1058 * @exception SocketException If the SocketImplFactory is already defined
1059 * @exception IOException If any other error occurs
1061 public static synchronized void setSocketImplFactory(SocketImplFactory fac
)
1064 // See if already set
1065 if (factory
!= null)
1066 throw new SocketException("SocketImplFactory already defined");
1068 // Check permissions
1069 SecurityManager sm
= System
.getSecurityManager();
1071 sm
.checkSetFactory();
1074 throw new SocketException("SocketImplFactory cannot be null");
1080 * Closes the input side of the socket stream.
1082 * @exception IOException If an error occurs.
1086 public void shutdownInput() throws IOException
1089 throw new SocketException("socket is closed");
1091 getImpl().shutdownInput();
1092 inputShutdown
= true;
1096 * Closes the output side of the socket stream.
1098 * @exception IOException If an error occurs.
1102 public void shutdownOutput() throws IOException
1105 throw new SocketException("socket is closed");
1107 getImpl().shutdownOutput();
1108 outputShutdown
= true;
1112 * Returns the socket channel associated with this socket.
1114 * @return the associated socket channel,
1115 * null if no associated channel exists
1119 public SocketChannel
getChannel()
1125 * Checks if the SO_REUSEADDR option is enabled
1127 * @return True if SO_REUSEADDR is set, false otherwise.
1129 * @exception SocketException If an error occurs
1133 public boolean getReuseAddress() throws SocketException
1136 throw new SocketException("socket is closed");
1138 Object reuseaddr
= getImpl().getOption(SocketOptions
.SO_REUSEADDR
);
1140 if (! (reuseaddr
instanceof Boolean
))
1141 throw new SocketException("Internal Error");
1143 return ((Boolean
) reuseaddr
).booleanValue();
1147 * Enables/Disables the SO_REUSEADDR option
1149 * @param reuseAddress true if SO_REUSEADDR should be enabled,
1152 * @exception SocketException If an error occurs
1156 public void setReuseAddress(boolean reuseAddress
) throws SocketException
1158 getImpl().setOption(SocketOptions
.SO_REUSEADDR
,
1159 Boolean
.valueOf(reuseAddress
));
1163 * Returns the current traffic class
1165 * @return The current traffic class.
1167 * @exception SocketException If an error occurs
1169 * @see Socket#setTrafficClass(int tc)
1173 public int getTrafficClass() throws SocketException
1176 throw new SocketException("socket is closed");
1178 Object obj
= getImpl().getOption(SocketOptions
.IP_TOS
);
1180 if (obj
instanceof Integer
)
1181 return ((Integer
) obj
).intValue();
1183 throw new SocketException("Unexpected type");
1187 * Sets the traffic class value
1189 * @param tc The traffic class
1191 * @exception SocketException If an error occurs
1192 * @exception IllegalArgumentException If tc value is illegal
1194 * @see Socket#getTrafficClass()
1198 public void setTrafficClass(int tc
) throws SocketException
1201 throw new SocketException("socket is closed");
1203 if (tc
< 0 || tc
> 255)
1204 throw new IllegalArgumentException();
1206 getImpl().setOption(SocketOptions
.IP_TOS
, new Integer(tc
));
1210 * Checks if the socket is connected
1212 * @return True if socket is connected, false otherwise.
1216 public boolean isConnected()
1220 return getImpl().getInetAddress() != null;
1222 catch (SocketException e
)
1229 * Checks if the socket is already bound.
1231 * @return True if socket is bound, false otherwise.
1235 public boolean isBound()
1241 * Checks if the socket is closed.
1243 * @return True if socket is closed, false otherwise.
1247 public boolean isClosed()
1249 return impl
== null;
1253 * Checks if the socket's input stream is shutdown
1255 * @return True if input is shut down.
1259 public boolean isInputShutdown()
1261 return inputShutdown
;
1265 * Checks if the socket's output stream is shutdown
1267 * @return True if output is shut down.
1271 public boolean isOutputShutdown()
1273 return outputShutdown
;