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
;
82 private boolean implCreated
= false;
84 private boolean inputShutdown
= false;
85 private boolean outputShutdown
= false;
88 * Initializes a new instance of <code>Socket</code> object without
89 * connecting to a remote host. This useful for subclasses of socket that
90 * might want this behavior.
92 * @specnote This constructor is public since JDK 1.4
98 impl
= factory
.createSocketImpl();
100 impl
= new PlainSocketImpl();
104 * Initializes a new instance of <code>Socket</code> object without
105 * connecting to a remote host. This is useful for subclasses of socket
106 * that might want this behavior.
108 * Additionally, this socket will be created using the supplied
109 * implementation class instead the default class or one returned by a
110 * factory. If this value is <code>null</code>, the default Socket
111 * implementation is used.
113 * @param impl The <code>SocketImpl</code> to use for this
114 * <code>Socket</code>
116 * @exception SocketException If an error occurs
120 protected Socket (SocketImpl impl
) throws SocketException
123 this.impl
= new PlainSocketImpl();
129 * Initializes a new instance of <code>Socket</code> and connects to the
130 * hostname and port specified as arguments.
132 * @param host The name of the host to connect to
133 * @param port The port number to connect to
135 * @exception UnknownHostException If the hostname cannot be resolved to a
137 * @exception IOException If an error occurs
138 * @exception SecurityException If a security manager exists and its
139 * checkConnect method doesn't allow the operation
141 public Socket (String host
, int port
)
142 throws UnknownHostException
, IOException
144 this(InetAddress
.getByName(host
), port
, null, 0, true);
148 * Initializes a new instance of <code>Socket</code> and connects to the
149 * address and port number specified as arguments.
151 * @param address The address to connect to
152 * @param port The port number to connect to
154 * @exception IOException If an error occurs
155 * @exception SecurityException If a security manager exists and its
156 * checkConnect method doesn't allow the operation
158 public Socket (InetAddress address
, int port
)
161 this(address
, port
, null, 0, true);
165 * Initializes a new instance of <code>Socket</code> that connects to the
166 * named host on the specified port and binds to the specified local address
169 * @param host The name of the remote host to connect to.
170 * @param port The remote port to connect to.
171 * @param localAddr The local address to bind to.
172 * @param localPort The local port to bind to.
174 * @exception SecurityException If the <code>SecurityManager</code>
175 * exists and does not allow a connection to the specified host/port or
176 * binding to the specified local host/port.
177 * @exception IOException If a connection error occurs.
181 public Socket (String host
, int port
,
182 InetAddress localAddr
, int localPort
) throws IOException
184 this(InetAddress
.getByName(host
), port
, localAddr
, localPort
, true);
188 * Initializes a new instance of <code>Socket</code> and connects to the
189 * address and port number specified as arguments, plus binds to the
190 * specified local address and port.
192 * @param address The remote address to connect to
193 * @param port The remote port to connect to
194 * @param localAddr The local address to connect to
195 * @param localPort The local port to connect to
197 * @exception IOException If an error occurs
198 * @exception SecurityException If a security manager exists and its
199 * checkConnect method doesn't allow the operation
203 public Socket (InetAddress address
, int port
,
204 InetAddress localAddr
, int localPort
) throws IOException
206 this(address
, port
, localAddr
, localPort
, true);
210 * Initializes a new instance of <code>Socket</code> and connects to the
211 * hostname and port specified as arguments. If the stream argument is set
212 * to <code>true</code>, then a stream socket is created. If it is
213 * <code>false</code>, a datagram socket is created.
215 * @param host The name of the host to connect to
216 * @param port The port to connect to
217 * @param stream <code>true</code> for a stream socket, <code>false</code>
218 * for a datagram socket
220 * @exception IOException If an error occurs
221 * @exception SecurityException If a security manager exists and its
222 * checkConnect method doesn't allow the operation
224 * @deprecated Use the <code>DatagramSocket</code> class to create
225 * datagram oriented sockets.
227 public Socket (String host
, int port
, boolean stream
) throws IOException
229 this(InetAddress
.getByName(host
), port
, null, 0, stream
);
233 * Initializes a new instance of <code>Socket</code> and connects to the
234 * address and port number specified as arguments. If the stream param is
235 * <code>true</code>, a stream socket will be created, otherwise a datagram
238 * @param host The address to connect to
239 * @param port The port number to connect to
240 * @param stream <code>true</code> to create a stream socket,
241 * <code>false</code> to create a datagram socket.
243 * @exception IOException If an error occurs
244 * @exception SecurityException If a security manager exists and its
245 * checkConnect method doesn't allow the operation
247 * @deprecated Use the <code>DatagramSocket</code> class to create
248 * datagram oriented sockets.
250 public Socket (InetAddress host
, int port
, boolean stream
) throws IOException
252 this(host
, port
, null, 0, stream
);
256 * This constructor is where the real work takes place. Connect to the
257 * specified address and port. Use default local values if not specified,
258 * otherwise use the local host and port passed in. Create as stream or
259 * datagram based on "stream" argument.
262 * @param raddr The remote address to connect to
263 * @param rport The remote port to connect to
264 * @param laddr The local address to connect to
265 * @param lport The local port to connect to
266 * @param stream true for a stream socket, false for a datagram socket
268 * @exception IOException If an error occurs
269 * @exception SecurityException If a security manager exists and its
270 * checkConnect method doesn't allow the operation
272 private Socket(InetAddress raddr
, int rport
, InetAddress laddr
, int lport
,
273 boolean stream
) throws IOException
277 SecurityManager sm
= System
.getSecurityManager();
279 sm
.checkConnect(raddr
.getHostName(), rport
);
282 SocketAddress bindaddr
=
283 laddr
== null ?
null : new InetSocketAddress (laddr
, lport
);
287 connect (new InetSocketAddress (raddr
, rport
));
289 // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
290 // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
291 // that default. JDK 1.2 doc infers not to do a bind.
294 // This has to be accessible from java.net.ServerSocket.
296 throws SocketException
306 catch (IOException e
)
308 throw new SocketException(e
.getMessage());
315 * Binds the socket to the givent local address/port
317 * @param bindpoint The address/port to bind to
319 * @exception IOException If an error occurs
320 * @exception SecurityException If a security manager exists and its
321 * checkConnect method doesn't allow the operation
322 * @exception IllegalArgumentException If the address type is not supported
326 public void bind (SocketAddress bindpoint
) throws IOException
329 throw new SocketException("socket is closed");
331 // XXX: JDK 1.4.1 API documentation says that if bindpoint is null the
332 // socket will be bound to an ephemeral port and a valid local address.
333 if (bindpoint
== null)
334 bindpoint
= new InetSocketAddress (InetAddress
.ANY_IF
, 0);
336 if ( !(bindpoint
instanceof InetSocketAddress
))
337 throw new IllegalArgumentException ();
339 InetSocketAddress tmp
= (InetSocketAddress
) bindpoint
;
341 // bind to address/port
344 getImpl().bind (tmp
.getAddress(), tmp
.getPort());
346 catch (IOException exception
)
351 catch (RuntimeException exception
)
364 * Connects the socket with a remote address.
366 * @param endpoint The address to connect to
368 * @exception IOException If an error occurs
369 * @exception IllegalArgumentException If the addess type is not supported
370 * @exception IllegalBlockingModeException If this socket has an associated
371 * channel, and the channel is in non-blocking mode
375 public void connect (SocketAddress endpoint
)
378 connect (endpoint
, 0);
382 * Connects the socket with a remote address. A timeout of zero is
383 * interpreted as an infinite timeout. The connection will then block
384 * until established or an error occurs.
386 * @param endpoint The address to connect to
387 * @param timeout The length of the timeout in milliseconds, or
388 * 0 to indicate no timeout.
390 * @exception IOException If an error occurs
391 * @exception IllegalArgumentException If the address type is not supported
392 * @exception IllegalBlockingModeException If this socket has an associated
393 * channel, and the channel is in non-blocking mode
394 * @exception SocketTimeoutException If the timeout is reached
398 public void connect (SocketAddress endpoint
, int timeout
)
402 throw new SocketException("socket is closed");
404 if (! (endpoint
instanceof InetSocketAddress
))
405 throw new IllegalArgumentException("unsupported address type");
407 if (getChannel() != null
408 && !getChannel().isBlocking ())
409 throw new IllegalBlockingModeException ();
416 getImpl().connect (endpoint
, timeout
);
418 catch (IOException exception
)
423 catch (RuntimeException exception
)
436 * Returns the address of the remote end of the socket. If this socket
437 * is not connected, then <code>null</code> is returned.
439 * @return The remote address this socket is connected to
441 public InetAddress
getInetAddress ()
448 return getImpl().getInetAddress();
450 catch (SocketException e
)
452 // This cannot happen as we are connected.
459 * Returns the local address to which this socket is bound. If this socket
460 * is not connected, then <code>null</code> is returned.
462 * @return The local address
466 public InetAddress
getLocalAddress ()
468 InetAddress addr
= null;
472 addr
= (InetAddress
) getImpl().getOption(SocketOptions
.SO_BINDADDR
);
474 catch(SocketException e
)
476 // (hopefully) shouldn't happen
477 // throw new java.lang.InternalError
478 // ("Error in PlainSocketImpl.getOption");
482 // FIXME: According to libgcj, checkConnect() is supposed to be called
483 // before performing this operation. Problems: 1) We don't have the
484 // addr until after we do it, so we do a post check. 2). The docs I
485 // see don't require this in the Socket case, only DatagramSocket, but
486 // we'll assume they mean both.
487 SecurityManager sm
= System
.getSecurityManager();
489 sm
.checkConnect(addr
.getHostName(), getLocalPort());
495 * Returns the port number of the remote end of the socket connection. If
496 * this socket is not connected, then -1 is returned.
498 * @return The remote port this socket is connected to
500 public int getPort ()
507 if (getImpl() != null)
508 return getImpl().getPort();
510 catch (SocketException e
)
512 // This cannot happen as we are connected.
519 * Returns the local port number to which this socket is bound. If this
520 * socket is not connected, then -1 is returned.
522 * @return The local port
524 public int getLocalPort ()
531 if (getImpl() != null)
532 return getImpl().getLocalPort();
534 catch (SocketException e
)
536 // This cannot happen as we are bound.
543 * If the socket is already bound this returns the local SocketAddress,
548 public SocketAddress
getLocalSocketAddress()
553 InetAddress addr
= getLocalAddress ();
557 return new InetSocketAddress (addr
, getImpl().getLocalPort());
559 catch (SocketException e
)
561 // This cannot happen as we are bound.
567 * If the socket is already connected this returns the remote SocketAddress,
572 public SocketAddress
getRemoteSocketAddress()
579 return new InetSocketAddress (getImpl().getInetAddress (), getImpl().getPort ());
581 catch (SocketException e
)
583 // This cannot happen as we are connected.
589 * Returns an InputStream for reading from this socket.
591 * @return The InputStream object
593 * @exception IOException If an error occurs or Socket is not connected
595 public InputStream
getInputStream () throws IOException
598 throw new SocketException("socket is closed");
601 throw new IOException("not connected");
603 return getImpl().getInputStream();
607 * Returns an OutputStream for writing to this socket.
609 * @return The OutputStream object
611 * @exception IOException If an error occurs or Socket is not connected
613 public OutputStream
getOutputStream () throws IOException
616 throw new SocketException("socket is closed");
619 throw new IOException("not connected");
621 return getImpl().getOutputStream();
625 * Sets the TCP_NODELAY option on the socket.
627 * @param on true to enable, false to disable
629 * @exception SocketException If an error occurs or Socket is not connected
633 public void setTcpNoDelay (boolean on
) throws SocketException
636 throw new SocketException("socket is closed");
638 getImpl().setOption(SocketOptions
.TCP_NODELAY
, new Boolean(on
));
642 * Tests whether or not the TCP_NODELAY option is set on the socket.
643 * Returns true if enabled, false if disabled. When on it disables the
644 * Nagle algorithm which means that packets are always send immediatly and
645 * never merged together to reduce network trafic.
647 * @return Whether or not TCP_NODELAY is set
649 * @exception SocketException If an error occurs or Socket not connected
653 public boolean getTcpNoDelay() throws SocketException
656 throw new SocketException("socket is closed");
658 Object on
= getImpl().getOption(SocketOptions
.TCP_NODELAY
);
660 if (on
instanceof Boolean
)
661 return(((Boolean
)on
).booleanValue());
663 throw new SocketException("Internal Error");
667 * Sets the value of the SO_LINGER option on the socket. If the
668 * SO_LINGER option is set on a socket and there is still data waiting to
669 * be sent when the socket is closed, then the close operation will block
670 * until either that data is delivered or until the timeout period
671 * expires. The linger interval is specified in hundreths of a second
672 * (platform specific?)
674 * @param on true to enable SO_LINGER, false to disable
675 * @param linger The SO_LINGER timeout in hundreths of a second or -1 if
678 * @exception SocketException If an error occurs or Socket not connected
679 * @exception IllegalArgumentException If linger is negative
683 public void setSoLinger(boolean on
, int linger
) throws SocketException
686 throw new SocketException("socket is closed");
691 throw new IllegalArgumentException("SO_LINGER must be >= 0");
696 getImpl().setOption(SocketOptions
.SO_LINGER
, new Integer(linger
));
700 getImpl().setOption(SocketOptions
.SO_LINGER
, new Boolean(false));
705 * Returns the value of the SO_LINGER option on the socket. If the
706 * SO_LINGER option is set on a socket and there is still data waiting to
707 * be sent when the socket is closed, then the close operation will block
708 * until either that data is delivered or until the timeout period
709 * expires. This method either returns the timeouts (in hundredths of
710 * of a second (platform specific?)) if SO_LINGER is set, or -1 if
711 * SO_LINGER is not set.
713 * @return The SO_LINGER timeout in hundreths of a second or -1
714 * if SO_LINGER not set
716 * @exception SocketException If an error occurs or Socket is not connected
720 public int getSoLinger() throws SocketException
723 throw new SocketException("socket is closed");
725 Object linger
= getImpl().getOption(SocketOptions
.SO_LINGER
);
727 if (linger
instanceof Integer
)
728 return(((Integer
)linger
).intValue());
734 * Sends urgent data through the socket
736 * @param data The data to send.
737 * Only the lowest eight bits of data are sent
739 * @exception IOException If an error occurs
743 public void sendUrgentData (int data
) throws IOException
746 throw new SocketException("socket is closed");
748 getImpl().sendUrgentData (data
);
752 * Enables/disables the SO_OOBINLINE option
754 * @param on True if SO_OOBLINE should be enabled
756 * @exception SocketException If an error occurs
760 public void setOOBInline (boolean on
) throws SocketException
763 throw new SocketException("socket is closed");
765 getImpl().setOption(SocketOptions
.SO_OOBINLINE
, new Boolean(on
));
769 * Returns the current setting of the SO_OOBINLINE option for this socket
771 * @return True if SO_OOBINLINE is set, false otherwise.
773 * @exception SocketException If an error occurs
777 public boolean getOOBInline () throws SocketException
780 throw new SocketException("socket is closed");
782 Object buf
= getImpl().getOption(SocketOptions
.SO_OOBINLINE
);
784 if (buf
instanceof Boolean
)
785 return(((Boolean
)buf
).booleanValue());
787 throw new SocketException("Internal Error: Unexpected type");
791 * Sets the value of the SO_TIMEOUT option on the socket. If this value
792 * is set, and an read/write is performed that does not complete within
793 * the timeout period, a short count is returned (or an EWOULDBLOCK signal
794 * would be sent in Unix if no data had been read). A value of 0 for
795 * this option implies that there is no timeout (ie, operations will
796 * block forever). On systems that have separate read and write timeout
797 * values, this method returns the read timeout. This
798 * value is in milliseconds.
800 * @param timeout The length of the timeout in milliseconds, or
801 * 0 to indicate no timeout.
803 * @exception SocketException If an error occurs or Socket not connected
807 public synchronized void setSoTimeout (int timeout
) throws SocketException
810 throw new SocketException("socket is closed");
813 throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
815 getImpl().setOption(SocketOptions
.SO_TIMEOUT
, new Integer(timeout
));
819 * Returns the value of the SO_TIMEOUT option on the socket. If this value
820 * is set, and an read/write is performed that does not complete within
821 * the timeout period, a short count is returned (or an EWOULDBLOCK signal
822 * would be sent in Unix if no data had been read). A value of 0 for
823 * this option implies that there is no timeout (ie, operations will
824 * block forever). On systems that have separate read and write timeout
825 * values, this method returns the read timeout. This
826 * value is in thousandths of a second (implementation specific?).
828 * @return The length of the timeout in thousandth's of a second or 0
831 * @exception SocketException If an error occurs or Socket not connected
835 public synchronized int getSoTimeout () throws SocketException
838 throw new SocketException("socket is closed");
840 Object timeout
= getImpl().getOption(SocketOptions
.SO_TIMEOUT
);
841 if (timeout
instanceof Integer
)
842 return(((Integer
)timeout
).intValue());
848 * This method sets the value for the system level socket option
849 * SO_SNDBUF to the specified value. Note that valid values for this
850 * option are specific to a given operating system.
852 * @param size The new send buffer size.
854 * @exception SocketException If an error occurs or Socket not connected
855 * @exception IllegalArgumentException If size is 0 or negative
859 public void setSendBufferSize (int size
) throws SocketException
862 throw new SocketException("socket is closed");
865 throw new IllegalArgumentException("SO_SNDBUF value must be > 0");
867 getImpl().setOption(SocketOptions
.SO_SNDBUF
, new Integer(size
));
871 * This method returns the value of the system level socket option
872 * SO_SNDBUF, which is used by the operating system to tune buffer
873 * sizes for data transfers.
875 * @return The send buffer size.
877 * @exception SocketException If an error occurs or socket not connected
881 public int getSendBufferSize () throws SocketException
884 throw new SocketException("socket is closed");
886 Object buf
= getImpl().getOption(SocketOptions
.SO_SNDBUF
);
888 if (buf
instanceof Integer
)
889 return(((Integer
)buf
).intValue());
891 throw new SocketException("Internal Error: Unexpected type");
895 * This method sets the value for the system level socket option
896 * SO_RCVBUF to the specified value. Note that valid values for this
897 * option are specific to a given operating system.
899 * @param size The new receive buffer size.
901 * @exception SocketException If an error occurs or Socket is not connected
902 * @exception IllegalArgumentException If size is 0 or negative
906 public void setReceiveBufferSize (int size
) throws SocketException
909 throw new SocketException("socket is closed");
912 throw new IllegalArgumentException("SO_RCVBUF value must be > 0");
914 getImpl().setOption(SocketOptions
.SO_RCVBUF
, new Integer(size
));
918 * This method returns the value of the system level socket option
919 * SO_RCVBUF, which is used by the operating system to tune buffer
920 * sizes for data transfers.
922 * @return The receive buffer size.
924 * @exception SocketException If an error occurs or Socket is not connected
928 public int getReceiveBufferSize () throws SocketException
931 throw new SocketException("socket is closed");
933 Object buf
= getImpl().getOption(SocketOptions
.SO_RCVBUF
);
935 if (buf
instanceof Integer
)
936 return(((Integer
)buf
).intValue());
938 throw new SocketException("Internal Error: Unexpected type");
942 * This method sets the value for the socket level socket option
945 * @param on True if SO_KEEPALIVE should be enabled
947 * @exception SocketException If an error occurs or Socket is not connected
951 public void setKeepAlive (boolean on
) throws SocketException
954 throw new SocketException("socket is closed");
956 getImpl().setOption(SocketOptions
.SO_KEEPALIVE
, new Boolean(on
));
960 * This method returns the value of the socket level socket option
963 * @return The setting
965 * @exception SocketException If an error occurs or Socket is not connected
969 public boolean getKeepAlive () throws SocketException
972 throw new SocketException("socket is closed");
974 Object buf
= getImpl().getOption(SocketOptions
.SO_KEEPALIVE
);
976 if (buf
instanceof Boolean
)
977 return(((Boolean
)buf
).booleanValue());
979 throw new SocketException("Internal Error: Unexpected type");
985 * @exception IOException If an error occurs
987 public synchronized void close () throws IOException
990 throw new SocketException("socket is closed");
994 if (getChannel() != null)
995 getChannel().close();
1001 * Converts this <code>Socket</code> to a <code>String</code>.
1003 * @return The <code>String</code> representation of this <code>Socket</code>
1005 public String
toString ()
1010 return ("Socket[addr=" + getImpl().getInetAddress()
1011 + ",port=" + getImpl().getPort()
1012 + ",localport=" + getImpl().getLocalPort()
1015 catch (SocketException e
)
1017 // This cannot happen as we are connected.
1020 return "Socket[unconnected]";
1024 * Sets the <code>SocketImplFactory</code>. This may be done only once per
1025 * virtual machine. Subsequent attempts will generate a
1026 * <code>SocketException</code>. Note that a <code>SecurityManager</code>
1027 * check is made prior to setting the factory. If
1028 * insufficient privileges exist to set the factory, then an
1029 * <code>IOException</code> will be thrown.
1031 * @exception SecurityException If the <code>SecurityManager</code> does
1032 * not allow this operation.
1033 * @exception SocketException If the SocketImplFactory is already defined
1034 * @exception IOException If any other error occurs
1036 public static synchronized void setSocketImplFactory (SocketImplFactory fac
)
1039 // See if already set
1040 if (factory
!= null)
1041 throw new SocketException("SocketImplFactory already defined");
1043 // Check permissions
1044 SecurityManager sm
= System
.getSecurityManager();
1046 sm
.checkSetFactory();
1049 throw new SocketException("SocketImplFactory cannot be null");
1055 * Closes the input side of the socket stream.
1057 * @exception IOException If an error occurs.
1061 public void shutdownInput() throws IOException
1064 throw new SocketException("socket is closed");
1066 getImpl().shutdownInput();
1067 inputShutdown
= true;
1071 * Closes the output side of the socket stream.
1073 * @exception IOException If an error occurs.
1077 public void shutdownOutput() throws IOException
1080 throw new SocketException("socket is closed");
1082 getImpl().shutdownOutput();
1083 outputShutdown
= true;
1087 * Returns the socket channel associated with this socket.
1089 * It returns null if no associated socket exists.
1093 public SocketChannel
getChannel()
1099 * Checks if the SO_REUSEADDR option is enabled
1101 * @return True if SO_REUSEADDR is set, false otherwise.
1103 * @exception SocketException If an error occurs
1107 public boolean getReuseAddress () throws SocketException
1110 throw new SocketException("socket is closed");
1112 Object reuseaddr
= getImpl().getOption (SocketOptions
.SO_REUSEADDR
);
1114 if (!(reuseaddr
instanceof Boolean
))
1115 throw new SocketException ("Internal Error");
1117 return ((Boolean
) reuseaddr
).booleanValue ();
1121 * Enables/Disables the SO_REUSEADDR option
1123 * @param reuseAddress True if SO_REUSEADDR should be set.
1125 * @exception SocketException If an error occurs
1129 public void setReuseAddress (boolean on
) throws SocketException
1131 getImpl().setOption (SocketOptions
.SO_REUSEADDR
, new Boolean (on
));
1135 * Returns the current traffic class
1137 * @return The current traffic class.
1139 * @exception SocketException If an error occurs
1141 * @see Socket#setTrafficClass(int tc)
1145 public int getTrafficClass () throws SocketException
1148 throw new SocketException("socket is closed");
1150 Object obj
= getImpl().getOption(SocketOptions
.IP_TOS
);
1152 if (obj
instanceof Integer
)
1153 return ((Integer
) obj
).intValue ();
1155 throw new SocketException ("Unexpected type");
1159 * Sets the traffic class value
1161 * @param tc The traffic class
1163 * @exception SocketException If an error occurs
1164 * @exception IllegalArgumentException If tc value is illegal
1166 * @see Socket#getTrafficClass()
1170 public void setTrafficClass (int tc
) throws SocketException
1173 throw new SocketException("socket is closed");
1175 if (tc
< 0 || tc
> 255)
1176 throw new IllegalArgumentException();
1178 getImpl().setOption (SocketOptions
.IP_TOS
, new Integer (tc
));
1182 * Checks if the socket is connected
1184 * @return True if socket is connected, false otherwise.
1188 public boolean isConnected ()
1192 return getImpl().getInetAddress () != null;
1194 catch (SocketException e
)
1201 * Checks if the socket is already bound.
1203 * @return True if socket is bound, false otherwise.
1207 public boolean isBound ()
1209 return getLocalAddress () != null;
1213 * Checks if the socket is closed.
1215 * @return True if socket is closed, false otherwise.
1219 public boolean isClosed ()
1221 return impl
== null;
1225 * Checks if the socket's input stream is shutdown
1227 * @return True if input is shut down.
1231 public boolean isInputShutdown ()
1233 return inputShutdown
;
1237 * Checks if the socket's output stream is shutdown
1239 * @return True if output is shut down.
1243 public boolean isOutputShutdown ()
1245 return outputShutdown
;