1 /* Socket.java -- Client socket implementation
2 Copyright (C) 1998, 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import gnu
.java
.net
.PlainSocketImpl
;
42 import java
.io
.InputStream
;
43 import java
.io
.IOException
;
44 import java
.io
.OutputStream
;
45 import java
.nio
.channels
.SocketChannel
;
46 import java
.nio
.channels
.IllegalBlockingModeException
;
48 /* Written using on-line Java Platform 1.2 API Specification.
49 * Status: I believe all methods are implemented.
53 * This class models a client site socket. A socket is a TCP/IP endpoint
54 * for network communications conceptually similar to a file handle.
56 * This class does not actually do any work. Instead, it redirects all of
57 * its calls to a socket implementation object which implements the
58 * <code>SocketImpl</code> interface. The implementation class is
59 * instantiated by factory class that implements the
60 * <code>SocketImplFactory interface</code>. A default
61 * factory is provided, however the factory may be set by a call to
62 * the <code>setSocketImplFactory</code> method. Note that this may only be
63 * done once per virtual machine. If a subsequent attempt is made to set the
64 * factory, a <code>SocketException</code> will be thrown.
66 * @author Aaron M. Renn (arenn@urbanophile.com)
67 * @author Per Bothner (bothner@cygnus.com)
72 * This is the user SocketImplFactory for this class. If this variable is
73 * null, a default factory is used.
75 static SocketImplFactory factory
;
78 * The implementation object to which calls are redirected
80 private SocketImpl impl
;
83 * True if socket implementation was created by calling their create() method.
85 private boolean implCreated
;
88 * True if the socket is bound.
90 private boolean bound
;
93 * True if input is shutdown.
95 private boolean inputShutdown
;
98 * True if output is shutdown.
100 private boolean outputShutdown
;
103 * Initializes a new instance of <code>Socket</code> object without
104 * connecting to a remote host. This useful for subclasses of socket that
105 * might want this behavior.
107 * @specnote This constructor is public since JDK 1.4
113 impl
= factory
.createSocketImpl();
115 impl
= new PlainSocketImpl();
119 * Initializes a new instance of <code>Socket</code> object without
120 * connecting to a remote host. This is useful for subclasses of socket
121 * that might want this behavior.
123 * Additionally, this socket will be created using the supplied
124 * implementation class instead the default class or one returned by a
125 * factory. If this value is <code>null</code>, the default Socket
126 * implementation is used.
128 * @param impl The <code>SocketImpl</code> to use for this
129 * <code>Socket</code>
131 * @exception SocketException If an error occurs
135 protected Socket (SocketImpl impl
) throws SocketException
138 this.impl
= new PlainSocketImpl();
144 * Initializes a new instance of <code>Socket</code> and connects to the
145 * hostname and port specified as arguments.
147 * @param host The name of the host to connect to
148 * @param port The port number to connect to
150 * @exception UnknownHostException If the hostname cannot be resolved to a
152 * @exception IOException If an error occurs
153 * @exception SecurityException If a security manager exists and its
154 * checkConnect method doesn't allow the operation
156 public Socket (String host
, int port
)
157 throws UnknownHostException
, IOException
159 this(InetAddress
.getByName(host
), port
, null, 0, true);
163 * Initializes a new instance of <code>Socket</code> and connects to the
164 * address and port number specified as arguments.
166 * @param address The address to connect to
167 * @param port The port number to connect to
169 * @exception IOException If an error occurs
170 * @exception SecurityException If a security manager exists and its
171 * checkConnect method doesn't allow the operation
173 public Socket (InetAddress address
, int port
)
176 this(address
, port
, null, 0, true);
180 * Initializes a new instance of <code>Socket</code> that connects to the
181 * named host on the specified port and binds to the specified local address
184 * @param host The name of the remote host to connect to.
185 * @param port The remote port to connect to.
186 * @param localAddr The local address to bind to.
187 * @param localPort The local port to bind to.
189 * @exception SecurityException If the <code>SecurityManager</code>
190 * exists and does not allow a connection to the specified host/port or
191 * binding to the specified local host/port.
192 * @exception IOException If a connection error occurs.
196 public Socket (String host
, int port
,
197 InetAddress localAddr
, int localPort
) throws IOException
199 this(InetAddress
.getByName(host
), port
, localAddr
, localPort
, true);
203 * Initializes a new instance of <code>Socket</code> and connects to the
204 * address and port number specified as arguments, plus binds to the
205 * specified local address and port.
207 * @param address The remote address to connect to
208 * @param port The remote port to connect to
209 * @param localAddr The local address to connect to
210 * @param localPort The local port to connect to
212 * @exception IOException If an error occurs
213 * @exception SecurityException If a security manager exists and its
214 * checkConnect method doesn't allow the operation
218 public Socket (InetAddress address
, int port
,
219 InetAddress localAddr
, int localPort
) throws IOException
221 this(address
, port
, localAddr
, localPort
, true);
225 * Initializes a new instance of <code>Socket</code> and connects to the
226 * hostname and port specified as arguments. If the stream argument is set
227 * to <code>true</code>, then a stream socket is created. If it is
228 * <code>false</code>, a datagram socket is created.
230 * @param host The name of the host to connect to
231 * @param port The port to connect to
232 * @param stream <code>true</code> for a stream socket, <code>false</code>
233 * for a datagram socket
235 * @exception IOException If an error occurs
236 * @exception SecurityException If a security manager exists and its
237 * checkConnect method doesn't allow the operation
239 * @deprecated Use the <code>DatagramSocket</code> class to create
240 * datagram oriented sockets.
242 public Socket (String host
, int port
, boolean stream
) throws IOException
244 this(InetAddress
.getByName(host
), port
, null, 0, stream
);
248 * Initializes a new instance of <code>Socket</code> and connects to the
249 * address and port number specified as arguments. If the stream param is
250 * <code>true</code>, a stream socket will be created, otherwise a datagram
253 * @param host The address to connect to
254 * @param port The port number to connect to
255 * @param stream <code>true</code> to create a stream socket,
256 * <code>false</code> to create a datagram socket.
258 * @exception IOException If an error occurs
259 * @exception SecurityException If a security manager exists and its
260 * checkConnect method doesn't allow the operation
262 * @deprecated Use the <code>DatagramSocket</code> class to create
263 * datagram oriented sockets.
265 public Socket (InetAddress host
, int port
, boolean stream
) throws IOException
267 this(host
, port
, null, 0, stream
);
271 * This constructor is where the real work takes place. Connect to the
272 * specified address and port. Use default local values if not specified,
273 * otherwise use the local host and port passed in. Create as stream or
274 * datagram based on "stream" argument.
277 * @param raddr The remote address to connect to
278 * @param rport The remote port to connect to
279 * @param laddr The local address to connect to
280 * @param lport The local port to connect to
281 * @param stream true for a stream socket, false for a datagram socket
283 * @exception IOException If an error occurs
284 * @exception SecurityException If a security manager exists and its
285 * checkConnect method doesn't allow the operation
287 private Socket(InetAddress raddr
, int rport
, InetAddress laddr
, int lport
,
288 boolean stream
) throws IOException
292 SecurityManager sm
= System
.getSecurityManager();
294 sm
.checkConnect(raddr
.getHostName(), rport
);
297 SocketAddress bindaddr
=
298 laddr
== null ?
null : new InetSocketAddress (laddr
, lport
);
302 connect (new InetSocketAddress (raddr
, rport
));
304 // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
305 // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
306 // that default. JDK 1.2 doc infers not to do a bind.
309 // This has to be accessible from java.net.ServerSocket.
311 throws SocketException
321 catch (IOException e
)
323 throw new SocketException(e
.getMessage());
330 * Binds the socket to the givent local address/port
332 * @param bindpoint The address/port to bind to
334 * @exception IOException If an error occurs
335 * @exception SecurityException If a security manager exists and its
336 * checkConnect method doesn't allow the operation
337 * @exception IllegalArgumentException If the address type is not supported
341 public void bind (SocketAddress bindpoint
) throws IOException
344 throw new SocketException("socket is closed");
346 // XXX: JDK 1.4.1 API documentation says that if bindpoint is null the
347 // socket will be bound to an ephemeral port and a valid local address.
348 if (bindpoint
== null)
349 bindpoint
= new InetSocketAddress (InetAddress
.ANY_IF
, 0);
351 if ( !(bindpoint
instanceof InetSocketAddress
))
352 throw new IllegalArgumentException ();
354 InetSocketAddress tmp
= (InetSocketAddress
) bindpoint
;
356 // bind to address/port
359 getImpl().bind (tmp
.getAddress(), tmp
.getPort());
362 catch (IOException exception
)
367 catch (RuntimeException exception
)
380 * Connects the socket with a remote address.
382 * @param endpoint The address to connect to
384 * @exception IOException If an error occurs
385 * @exception IllegalArgumentException If the addess type is not supported
386 * @exception IllegalBlockingModeException If this socket has an associated
387 * channel, and the channel is in non-blocking mode
391 public void connect (SocketAddress endpoint
)
394 connect (endpoint
, 0);
398 * Connects the socket with a remote address. A timeout of zero is
399 * interpreted as an infinite timeout. The connection will then block
400 * until established or an error occurs.
402 * @param endpoint The address to connect to
403 * @param timeout The length of the timeout in milliseconds, or
404 * 0 to indicate no timeout.
406 * @exception IOException If an error occurs
407 * @exception IllegalArgumentException If the address type is not supported
408 * @exception IllegalBlockingModeException If this socket has an associated
409 * channel, and the channel is in non-blocking mode
410 * @exception SocketTimeoutException If the timeout is reached
414 public void connect (SocketAddress endpoint
, int timeout
)
418 throw new SocketException("socket is closed");
420 if (! (endpoint
instanceof InetSocketAddress
))
421 throw new IllegalArgumentException("unsupported address type");
423 if (getChannel() != null
424 && !getChannel().isBlocking ())
425 throw new IllegalBlockingModeException ();
432 getImpl().connect (endpoint
, timeout
);
434 catch (IOException exception
)
439 catch (RuntimeException exception
)
452 * Returns the address of the remote end of the socket. If this socket
453 * is not connected, then <code>null</code> is returned.
455 * @return The remote address this socket is connected to
457 public InetAddress
getInetAddress ()
464 return getImpl().getInetAddress();
466 catch (SocketException e
)
468 // This cannot happen as we are connected.
475 * Returns the local address to which this socket is bound. If this socket
476 * is not connected, then <code>null</code> is returned.
478 * @return The local address
482 public InetAddress
getLocalAddress ()
484 InetAddress addr
= null;
488 addr
= (InetAddress
) getImpl().getOption(SocketOptions
.SO_BINDADDR
);
490 catch(SocketException e
)
492 // (hopefully) shouldn't happen
493 // throw new java.lang.InternalError
494 // ("Error in PlainSocketImpl.getOption");
498 // FIXME: According to libgcj, checkConnect() is supposed to be called
499 // before performing this operation. Problems: 1) We don't have the
500 // addr until after we do it, so we do a post check. 2). The docs I
501 // see don't require this in the Socket case, only DatagramSocket, but
502 // we'll assume they mean both.
503 SecurityManager sm
= System
.getSecurityManager();
505 sm
.checkConnect(addr
.getHostName(), getLocalPort());
511 * Returns the port number of the remote end of the socket connection. If
512 * this socket is not connected, then -1 is returned.
514 * @return The remote port this socket is connected to
516 public int getPort ()
523 if (getImpl() != null)
524 return getImpl().getPort();
526 catch (SocketException e
)
528 // This cannot happen as we are connected.
535 * Returns the local port number to which this socket is bound. If this
536 * socket is not connected, then -1 is returned.
538 * @return The local port
540 public int getLocalPort ()
547 if (getImpl() != null)
548 return getImpl().getLocalPort();
550 catch (SocketException e
)
552 // This cannot happen as we are bound.
559 * If the socket is already bound this returns the local SocketAddress,
564 public SocketAddress
getLocalSocketAddress()
569 InetAddress addr
= getLocalAddress ();
573 return new InetSocketAddress (addr
, getImpl().getLocalPort());
575 catch (SocketException e
)
577 // This cannot happen as we are bound.
583 * If the socket is already connected this returns the remote SocketAddress,
588 public SocketAddress
getRemoteSocketAddress()
595 return new InetSocketAddress (getImpl().getInetAddress (), getImpl().getPort ());
597 catch (SocketException e
)
599 // This cannot happen as we are connected.
605 * Returns an InputStream for reading from this socket.
607 * @return The InputStream object
609 * @exception IOException If an error occurs or Socket is not connected
611 public InputStream
getInputStream () throws IOException
614 throw new SocketException("socket is closed");
617 throw new IOException("not connected");
619 return getImpl().getInputStream();
623 * Returns an OutputStream for writing to this socket.
625 * @return The OutputStream object
627 * @exception IOException If an error occurs or Socket is not connected
629 public OutputStream
getOutputStream () throws IOException
632 throw new SocketException("socket is closed");
635 throw new IOException("not connected");
637 return getImpl().getOutputStream();
641 * Sets the TCP_NODELAY option on the socket.
643 * @param on true to enable, false to disable
645 * @exception SocketException If an error occurs or Socket is not connected
649 public void setTcpNoDelay (boolean on
) throws SocketException
652 throw new SocketException("socket is closed");
654 getImpl().setOption(SocketOptions
.TCP_NODELAY
, new Boolean(on
));
658 * Tests whether or not the TCP_NODELAY option is set on the socket.
659 * Returns true if enabled, false if disabled. When on it disables the
660 * Nagle algorithm which means that packets are always send immediatly and
661 * never merged together to reduce network trafic.
663 * @return Whether or not TCP_NODELAY is set
665 * @exception SocketException If an error occurs or Socket not connected
669 public boolean getTcpNoDelay() throws SocketException
672 throw new SocketException("socket is closed");
674 Object on
= getImpl().getOption(SocketOptions
.TCP_NODELAY
);
676 if (on
instanceof Boolean
)
677 return(((Boolean
)on
).booleanValue());
679 throw new SocketException("Internal Error");
683 * Sets the value of the SO_LINGER option on the socket. If the
684 * SO_LINGER option is set on a socket and there is still data waiting to
685 * be sent when the socket is closed, then the close operation will block
686 * until either that data is delivered or until the timeout period
687 * expires. The linger interval is specified in hundreths of a second
688 * (platform specific?)
690 * @param on true to enable SO_LINGER, false to disable
691 * @param linger The SO_LINGER timeout in hundreths of a second or -1 if
694 * @exception SocketException If an error occurs or Socket not connected
695 * @exception IllegalArgumentException If linger is negative
699 public void setSoLinger(boolean on
, int linger
) throws SocketException
702 throw new SocketException("socket is closed");
707 throw new IllegalArgumentException("SO_LINGER must be >= 0");
712 getImpl().setOption(SocketOptions
.SO_LINGER
, new Integer(linger
));
716 getImpl().setOption(SocketOptions
.SO_LINGER
, new Boolean(false));
721 * Returns the value of the SO_LINGER option on the socket. If the
722 * SO_LINGER option is set on a socket and there is still data waiting to
723 * be sent when the socket is closed, then the close operation will block
724 * until either that data is delivered or until the timeout period
725 * expires. This method either returns the timeouts (in hundredths of
726 * of a second (platform specific?)) if SO_LINGER is set, or -1 if
727 * SO_LINGER is not set.
729 * @return The SO_LINGER timeout in hundreths of a second or -1
730 * if SO_LINGER not set
732 * @exception SocketException If an error occurs or Socket is not connected
736 public int getSoLinger() throws SocketException
739 throw new SocketException("socket is closed");
741 Object linger
= getImpl().getOption(SocketOptions
.SO_LINGER
);
743 if (linger
instanceof Integer
)
744 return(((Integer
)linger
).intValue());
750 * Sends urgent data through the socket
752 * @param data The data to send.
753 * Only the lowest eight bits of data are sent
755 * @exception IOException If an error occurs
759 public void sendUrgentData (int data
) throws IOException
762 throw new SocketException("socket is closed");
764 getImpl().sendUrgentData (data
);
768 * Enables/disables the SO_OOBINLINE option
770 * @param on True if SO_OOBLINE should be enabled
772 * @exception SocketException If an error occurs
776 public void setOOBInline (boolean on
) throws SocketException
779 throw new SocketException("socket is closed");
781 getImpl().setOption(SocketOptions
.SO_OOBINLINE
, new Boolean(on
));
785 * Returns the current setting of the SO_OOBINLINE option for this socket
787 * @return True if SO_OOBINLINE is set, false otherwise.
789 * @exception SocketException If an error occurs
793 public boolean getOOBInline () throws SocketException
796 throw new SocketException("socket is closed");
798 Object buf
= getImpl().getOption(SocketOptions
.SO_OOBINLINE
);
800 if (buf
instanceof Boolean
)
801 return(((Boolean
)buf
).booleanValue());
803 throw new SocketException("Internal Error: Unexpected type");
807 * Sets the value of the SO_TIMEOUT option on the socket. If this value
808 * is set, and an read/write is performed that does not complete within
809 * the timeout period, a short count is returned (or an EWOULDBLOCK signal
810 * would be sent in Unix if no data had been read). A value of 0 for
811 * this option implies that there is no timeout (ie, operations will
812 * block forever). On systems that have separate read and write timeout
813 * values, this method returns the read timeout. This
814 * value is in milliseconds.
816 * @param timeout The length of the timeout in milliseconds, or
817 * 0 to indicate no timeout.
819 * @exception SocketException If an error occurs or Socket not connected
823 public synchronized void setSoTimeout (int timeout
) throws SocketException
826 throw new SocketException("socket is closed");
829 throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
831 getImpl().setOption(SocketOptions
.SO_TIMEOUT
, new Integer(timeout
));
835 * Returns the value of the SO_TIMEOUT option on the socket. If this value
836 * is set, and an read/write is performed that does not complete within
837 * the timeout period, a short count is returned (or an EWOULDBLOCK signal
838 * would be sent in Unix if no data had been read). A value of 0 for
839 * this option implies that there is no timeout (ie, operations will
840 * block forever). On systems that have separate read and write timeout
841 * values, this method returns the read timeout. This
842 * value is in thousandths of a second (implementation specific?).
844 * @return The length of the timeout in thousandth's of a second or 0
847 * @exception SocketException If an error occurs or Socket not connected
851 public synchronized int getSoTimeout () throws SocketException
854 throw new SocketException("socket is closed");
856 Object timeout
= getImpl().getOption(SocketOptions
.SO_TIMEOUT
);
857 if (timeout
instanceof Integer
)
858 return(((Integer
)timeout
).intValue());
864 * This method sets the value for the system level socket option
865 * SO_SNDBUF to the specified value. Note that valid values for this
866 * option are specific to a given operating system.
868 * @param size The new send buffer size.
870 * @exception SocketException If an error occurs or Socket not connected
871 * @exception IllegalArgumentException If size is 0 or negative
875 public void setSendBufferSize (int size
) throws SocketException
878 throw new SocketException("socket is closed");
881 throw new IllegalArgumentException("SO_SNDBUF value must be > 0");
883 getImpl().setOption(SocketOptions
.SO_SNDBUF
, new Integer(size
));
887 * This method returns the value of the system level socket option
888 * SO_SNDBUF, which is used by the operating system to tune buffer
889 * sizes for data transfers.
891 * @return The send buffer size.
893 * @exception SocketException If an error occurs or socket not connected
897 public int getSendBufferSize () throws SocketException
900 throw new SocketException("socket is closed");
902 Object buf
= getImpl().getOption(SocketOptions
.SO_SNDBUF
);
904 if (buf
instanceof Integer
)
905 return(((Integer
)buf
).intValue());
907 throw new SocketException("Internal Error: Unexpected type");
911 * This method sets the value for the system level socket option
912 * SO_RCVBUF to the specified value. Note that valid values for this
913 * option are specific to a given operating system.
915 * @param size The new receive buffer size.
917 * @exception SocketException If an error occurs or Socket is not connected
918 * @exception IllegalArgumentException If size is 0 or negative
922 public void setReceiveBufferSize (int size
) throws SocketException
925 throw new SocketException("socket is closed");
928 throw new IllegalArgumentException("SO_RCVBUF value must be > 0");
930 getImpl().setOption(SocketOptions
.SO_RCVBUF
, new Integer(size
));
934 * This method returns the value of the system level socket option
935 * SO_RCVBUF, which is used by the operating system to tune buffer
936 * sizes for data transfers.
938 * @return The receive buffer size.
940 * @exception SocketException If an error occurs or Socket is not connected
944 public int getReceiveBufferSize () throws SocketException
947 throw new SocketException("socket is closed");
949 Object buf
= getImpl().getOption(SocketOptions
.SO_RCVBUF
);
951 if (buf
instanceof Integer
)
952 return(((Integer
)buf
).intValue());
954 throw new SocketException("Internal Error: Unexpected type");
958 * This method sets the value for the socket level socket option
961 * @param on True if SO_KEEPALIVE should be enabled
963 * @exception SocketException If an error occurs or Socket is not connected
967 public void setKeepAlive (boolean on
) throws SocketException
970 throw new SocketException("socket is closed");
972 getImpl().setOption(SocketOptions
.SO_KEEPALIVE
, new Boolean(on
));
976 * This method returns the value of the socket level socket option
979 * @return The setting
981 * @exception SocketException If an error occurs or Socket is not connected
985 public boolean getKeepAlive () throws SocketException
988 throw new SocketException("socket is closed");
990 Object buf
= getImpl().getOption(SocketOptions
.SO_KEEPALIVE
);
992 if (buf
instanceof Boolean
)
993 return(((Boolean
)buf
).booleanValue());
995 throw new SocketException("Internal Error: Unexpected type");
1001 * @exception IOException If an error occurs
1003 public synchronized void close () throws IOException
1012 if (getChannel() != null)
1013 getChannel().close();
1017 * Converts this <code>Socket</code> to a <code>String</code>.
1019 * @return The <code>String</code> representation of this <code>Socket</code>
1021 public String
toString ()
1026 return ("Socket[addr=" + getImpl().getInetAddress()
1027 + ",port=" + getImpl().getPort()
1028 + ",localport=" + getImpl().getLocalPort()
1031 catch (SocketException e
)
1033 // This cannot happen as we are connected.
1036 return "Socket[unconnected]";
1040 * Sets the <code>SocketImplFactory</code>. This may be done only once per
1041 * virtual machine. Subsequent attempts will generate a
1042 * <code>SocketException</code>. Note that a <code>SecurityManager</code>
1043 * check is made prior to setting the factory. If
1044 * insufficient privileges exist to set the factory, then an
1045 * <code>IOException</code> will be thrown.
1047 * @exception SecurityException If the <code>SecurityManager</code> does
1048 * not allow this operation.
1049 * @exception SocketException If the SocketImplFactory is already defined
1050 * @exception IOException If any other error occurs
1052 public static synchronized void setSocketImplFactory (SocketImplFactory fac
)
1055 // See if already set
1056 if (factory
!= null)
1057 throw new SocketException("SocketImplFactory already defined");
1059 // Check permissions
1060 SecurityManager sm
= System
.getSecurityManager();
1062 sm
.checkSetFactory();
1065 throw new SocketException("SocketImplFactory cannot be null");
1071 * Closes the input side of the socket stream.
1073 * @exception IOException If an error occurs.
1077 public void shutdownInput() throws IOException
1080 throw new SocketException("socket is closed");
1082 getImpl().shutdownInput();
1083 inputShutdown
= true;
1087 * Closes the output side of the socket stream.
1089 * @exception IOException If an error occurs.
1093 public void shutdownOutput() throws IOException
1096 throw new SocketException("socket is closed");
1098 getImpl().shutdownOutput();
1099 outputShutdown
= true;
1103 * Returns the socket channel associated with this socket.
1105 * It returns null if no associated socket exists.
1109 public SocketChannel
getChannel()
1115 * Checks if the SO_REUSEADDR option is enabled
1117 * @return True if SO_REUSEADDR is set, false otherwise.
1119 * @exception SocketException If an error occurs
1123 public boolean getReuseAddress () throws SocketException
1126 throw new SocketException("socket is closed");
1128 Object reuseaddr
= getImpl().getOption (SocketOptions
.SO_REUSEADDR
);
1130 if (!(reuseaddr
instanceof Boolean
))
1131 throw new SocketException ("Internal Error");
1133 return ((Boolean
) reuseaddr
).booleanValue ();
1137 * Enables/Disables the SO_REUSEADDR option
1139 * @param reuseAddress True if SO_REUSEADDR should be set.
1141 * @exception SocketException If an error occurs
1145 public void setReuseAddress (boolean on
) throws SocketException
1147 getImpl().setOption (SocketOptions
.SO_REUSEADDR
, new Boolean (on
));
1151 * Returns the current traffic class
1153 * @return The current traffic class.
1155 * @exception SocketException If an error occurs
1157 * @see Socket#setTrafficClass(int tc)
1161 public int getTrafficClass () throws SocketException
1164 throw new SocketException("socket is closed");
1166 Object obj
= getImpl().getOption(SocketOptions
.IP_TOS
);
1168 if (obj
instanceof Integer
)
1169 return ((Integer
) obj
).intValue ();
1171 throw new SocketException ("Unexpected type");
1175 * Sets the traffic class value
1177 * @param tc The traffic class
1179 * @exception SocketException If an error occurs
1180 * @exception IllegalArgumentException If tc value is illegal
1182 * @see Socket#getTrafficClass()
1186 public void setTrafficClass (int tc
) throws SocketException
1189 throw new SocketException("socket is closed");
1191 if (tc
< 0 || tc
> 255)
1192 throw new IllegalArgumentException();
1194 getImpl().setOption (SocketOptions
.IP_TOS
, new Integer (tc
));
1198 * Checks if the socket is connected
1200 * @return True if socket is connected, false otherwise.
1204 public boolean isConnected ()
1208 return getImpl().getInetAddress () != null;
1210 catch (SocketException e
)
1217 * Checks if the socket is already bound.
1219 * @return True if socket is bound, false otherwise.
1223 public boolean isBound ()
1229 * Checks if the socket is closed.
1231 * @return True if socket is closed, false otherwise.
1235 public boolean isClosed ()
1237 return impl
== null;
1241 * Checks if the socket's input stream is shutdown
1243 * @return True if input is shut down.
1247 public boolean isInputShutdown ()
1249 return inputShutdown
;
1253 * Checks if the socket's output stream is shutdown
1255 * @return True if output is shut down.
1259 public boolean isOutputShutdown ()
1261 return outputShutdown
;