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., 51 Franklin Street, Fifth Floor, 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;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.io.OutputStream;
46 import java.nio.channels.IllegalBlockingModeException;
47 import java.nio.channels.SocketChannel;
50 /* Written using on-line Java Platform 1.2 API Specification.
51 * Status: I believe all methods are implemented.
55 * This class models a client site socket. A socket is a TCP/IP endpoint
56 * for network communications conceptually similar to a file handle.
58 * This class does not actually do any work. Instead, it redirects all of
59 * its calls to a socket implementation object which implements the
60 * <code>SocketImpl</code> interface. The implementation class is
61 * instantiated by factory class that implements the
62 * <code>SocketImplFactory interface</code>. A default
63 * factory is provided, however the factory may be set by a call to
64 * the <code>setSocketImplFactory</code> method. Note that this may only be
65 * done once per virtual machine. If a subsequent attempt is made to set the
66 * factory, a <code>SocketException</code> will be thrown.
68 * @author Aaron M. Renn (arenn@urbanophile.com)
69 * @author Per Bothner (bothner@cygnus.com)
74 * This is the user SocketImplFactory for this class. If this variable is
75 * null, a default factory is used.
77 static SocketImplFactory factory;
80 * The implementation object to which calls are redirected
82 // package-private because ServerSocket.implAccept() needs to access it.
86 * True if socket implementation was created by calling their
89 // package-private because ServerSocket.implAccept() needs to access it.
93 * True if the socket is bound.
94 * Package private so it can be set from ServerSocket when accept is called.
99 * True if input is shutdown.
101 private boolean inputShutdown;
104 * True if output is shutdown.
106 private boolean outputShutdown;
109 * Initializes a new instance of <code>Socket</code> object without
110 * connecting to a remote host. This useful for subclasses of socket that
111 * might want this behavior.
113 * @specnote This constructor is public since JDK 1.4
119 impl = factory.createSocketImpl();
121 impl = new PlainSocketImpl();
125 * Initializes a new instance of <code>Socket</code> object without
126 * connecting to a remote host. This is useful for subclasses of socket
127 * that might want this behavior.
129 * Additionally, this socket will be created using the supplied
130 * implementation class instead the default class or one returned by a
131 * factory. If this value is <code>null</code>, the default Socket
132 * implementation is used.
134 * @param impl The <code>SocketImpl</code> to use for this
135 * <code>Socket</code>
137 * @exception SocketException If an error occurs
141 protected Socket(SocketImpl impl) throws SocketException
144 this.impl = new PlainSocketImpl();
150 * Initializes a new instance of <code>Socket</code> and connects to the
151 * hostname and port specified as arguments.
153 * @param host The name of the host to connect to
154 * @param port The port number to connect to
156 * @exception UnknownHostException If the hostname cannot be resolved to a
158 * @exception IOException If an error occurs
159 * @exception SecurityException If a security manager exists and its
160 * checkConnect method doesn't allow the operation
162 public Socket(String host, int port)
163 throws UnknownHostException, IOException
165 this(InetAddress.getByName(host), port, null, 0, true);
169 * Initializes a new instance of <code>Socket</code> and connects to the
170 * address and port number specified as arguments.
172 * @param address The address to connect to
173 * @param port The port number to connect to
175 * @exception IOException If an error occurs
176 * @exception SecurityException If a security manager exists and its
177 * checkConnect method doesn't allow the operation
179 public Socket(InetAddress address, int port) throws IOException
181 this(address, port, null, 0, true);
185 * Initializes a new instance of <code>Socket</code> that connects to the
186 * named host on the specified port and binds to the specified local address
189 * @param host The name of the remote host to connect to.
190 * @param port The remote port to connect to.
191 * @param localAddr The local address to bind to.
192 * @param localPort The local port to bind to.
194 * @exception SecurityException If the <code>SecurityManager</code>
195 * exists and does not allow a connection to the specified host/port or
196 * binding to the specified local host/port.
197 * @exception IOException If a connection error occurs.
201 public Socket(String host, int port, InetAddress localAddr, int localPort)
204 this(InetAddress.getByName(host), port, localAddr, localPort, true);
208 * Initializes a new instance of <code>Socket</code> and connects to the
209 * address and port number specified as arguments, plus binds to the
210 * specified local address and port.
212 * @param address The remote address to connect to
213 * @param port The remote port to connect to
214 * @param localAddr The local address to connect to
215 * @param localPort The local port to connect to
217 * @exception IOException If an error occurs
218 * @exception SecurityException If a security manager exists and its
219 * checkConnect method doesn't allow the operation
223 public Socket(InetAddress address, int port, InetAddress localAddr,
224 int localPort) throws IOException
226 this(address, port, localAddr, localPort, true);
230 * Initializes a new instance of <code>Socket</code> and connects to the
231 * hostname and port specified as arguments. If the stream argument is set
232 * to <code>true</code>, then a stream socket is created. If it is
233 * <code>false</code>, a datagram socket is created.
235 * @param host The name of the host to connect to
236 * @param port The port to connect to
237 * @param stream <code>true</code> for a stream socket, <code>false</code>
238 * for a datagram socket
240 * @exception IOException If an error occurs
241 * @exception SecurityException If a security manager exists and its
242 * checkConnect method doesn't allow the operation
244 * @deprecated Use the <code>DatagramSocket</code> class to create
245 * datagram oriented sockets.
247 public Socket(String host, int port, boolean stream)
250 this(InetAddress.getByName(host), port, null, 0, stream);
254 * Initializes a new instance of <code>Socket</code> and connects to the
255 * address and port number specified as arguments. If the stream param is
256 * <code>true</code>, a stream socket will be created, otherwise a datagram
259 * @param host The address to connect to
260 * @param port The port number to connect to
261 * @param stream <code>true</code> to create a stream socket,
262 * <code>false</code> to create a datagram socket.
264 * @exception IOException If an error occurs
265 * @exception SecurityException If a security manager exists and its
266 * checkConnect method doesn't allow the operation
268 * @deprecated Use the <code>DatagramSocket</code> class to create
269 * datagram oriented sockets.
271 public Socket(InetAddress host, int port, boolean stream)
274 this(host, port, null, 0, stream);
278 * This constructor is where the real work takes place. Connect to the
279 * specified address and port. Use default local values if not specified,
280 * otherwise use the local host and port passed in. Create as stream or
281 * datagram based on "stream" argument.
284 * @param raddr The remote address to connect to
285 * @param rport The remote port to connect to
286 * @param laddr The local address to connect to
287 * @param lport The local port to connect to
288 * @param stream true for a stream socket, false for a datagram socket
290 * @exception IOException If an error occurs
291 * @exception SecurityException If a security manager exists and its
292 * checkConnect method doesn't allow the operation
294 private Socket(InetAddress raddr, int rport, InetAddress laddr, int lport,
295 boolean stream) throws IOException
299 SecurityManager sm = System.getSecurityManager();
301 sm.checkConnect(raddr.getHostName(), rport);
304 SocketAddress bindaddr =
305 laddr == null ? null : new InetSocketAddress(laddr, lport);
309 connect(new InetSocketAddress(raddr, rport));
311 // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
312 // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
313 // that default. JDK 1.2 doc infers not to do a bind.
316 private SocketImpl getImpl() throws SocketException
326 catch (IOException e)
328 SocketException se = new SocketException(e.toString());
337 * Binds the socket to the givent local address/port
339 * @param bindpoint The address/port to bind to
341 * @exception IOException If an error occurs
342 * @exception SecurityException If a security manager exists and its
343 * checkConnect method doesn't allow the operation
344 * @exception IllegalArgumentException If the address type is not supported
348 public void bind(SocketAddress bindpoint) throws IOException
351 throw new SocketException("socket is closed");
353 // XXX: JDK 1.4.1 API documentation says that if bindpoint is null the
354 // socket will be bound to an ephemeral port and a valid local address.
355 if (bindpoint == null)
356 bindpoint = new InetSocketAddress(InetAddress.ANY_IF, 0);
358 if (! (bindpoint instanceof InetSocketAddress))
359 throw new IllegalArgumentException();
361 InetSocketAddress tmp = (InetSocketAddress) bindpoint;
363 // bind to address/port
366 getImpl().bind(tmp.getAddress(), tmp.getPort());
369 catch (IOException exception)
374 catch (RuntimeException exception)
387 * Connects the socket with a remote address.
389 * @param endpoint The address to connect to
391 * @exception IOException If an error occurs
392 * @exception IllegalArgumentException If the addess type is not supported
393 * @exception IllegalBlockingModeException If this socket has an associated
394 * channel, and the channel is in non-blocking mode
398 public void connect(SocketAddress endpoint) throws IOException
400 connect(endpoint, 0);
404 * Connects the socket with a remote address. A timeout of zero is
405 * interpreted as an infinite timeout. The connection will then block
406 * until established or an error occurs.
408 * @param endpoint The address to connect to
409 * @param timeout The length of the timeout in milliseconds, or
410 * 0 to indicate no timeout.
412 * @exception IOException If an error occurs
413 * @exception IllegalArgumentException If the address type is not supported
414 * @exception IllegalBlockingModeException If this socket has an associated
415 * channel, and the channel is in non-blocking mode
416 * @exception SocketTimeoutException If the timeout is reached
420 public void connect(SocketAddress endpoint, int timeout)
424 throw new SocketException("socket is closed");
426 if (! (endpoint instanceof InetSocketAddress))
427 throw new IllegalArgumentException("unsupported address type");
429 // The Sun spec says that if we have an associated channel and
430 // it is in non-blocking mode, we throw an IllegalBlockingModeException.
431 // However, in our implementation if the channel itself initiated this
432 // operation, then we must honor it regardless of its blocking mode.
433 if (getChannel() != null && ! getChannel().isBlocking()
434 && ! ((PlainSocketImpl) getImpl()).isInChannelOperation())
435 throw new IllegalBlockingModeException();
440 getImpl().connect(endpoint, timeout);
444 * Returns the address of the remote end of the socket. If this socket
445 * is not connected, then <code>null</code> is returned.
447 * @return The remote address this socket is connected to
449 public InetAddress getInetAddress()
456 return getImpl().getInetAddress();
458 catch (SocketException e)
460 // This cannot happen as we are connected.
467 * Returns the local address to which this socket is bound. If this socket
468 * is not connected, then a wildcard address, for which
469 * @see InetAddress#isAnyLocalAddress() is <code>true</code>, is returned.
471 * @return The local address
475 public InetAddress getLocalAddress()
478 return InetAddress.ANY_IF;
480 InetAddress addr = null;
484 addr = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR);
486 catch (SocketException e)
488 // (hopefully) shouldn't happen
489 // throw new java.lang.InternalError
490 // ("Error in PlainSocketImpl.getOption");
494 // FIXME: According to libgcj, checkConnect() is supposed to be called
495 // before performing this operation. Problems: 1) We don't have the
496 // addr until after we do it, so we do a post check. 2). The docs I
497 // see don't require this in the Socket case, only DatagramSocket, but
498 // we'll assume they mean both.
499 SecurityManager sm = System.getSecurityManager();
501 sm.checkConnect(addr.getHostName(), getLocalPort());
507 * Returns the port number of the remote end of the socket connection. If
508 * this socket is not connected, then 0 is returned.
510 * @return The remote port this socket is connected to
519 return getImpl().getPort();
521 catch (SocketException e)
523 // This cannot happen as we are connected.
530 * Returns the local port number to which this socket is bound. If this
531 * socket is not connected, then -1 is returned.
533 * @return The local port
535 public int getLocalPort()
542 if (getImpl() != null)
543 return getImpl().getLocalPort();
545 catch (SocketException e)
547 // This cannot happen as we are bound.
554 * Returns local socket address.
556 * @return the local socket address, null if not bound
560 public SocketAddress getLocalSocketAddress()
565 InetAddress addr = getLocalAddress();
569 return new InetSocketAddress(addr, getImpl().getLocalPort());
571 catch (SocketException e)
573 // This cannot happen as we are bound.
579 * Returns the remote socket address.
581 * @return the remote socket address, null of not connected
585 public SocketAddress getRemoteSocketAddress()
592 return new InetSocketAddress(getImpl().getInetAddress(),
593 getImpl().getPort());
595 catch (SocketException e)
597 // This cannot happen as we are connected.
603 * Returns an InputStream for reading from this socket.
605 * @return The InputStream object
607 * @exception IOException If an error occurs or Socket is not connected
609 public InputStream getInputStream() throws IOException
612 throw new SocketException("socket is closed");
615 throw new IOException("not connected");
617 return getImpl().getInputStream();
621 * Returns an OutputStream for writing to this socket.
623 * @return The OutputStream object
625 * @exception IOException If an error occurs or Socket is not connected
627 public OutputStream getOutputStream() throws IOException
630 throw new SocketException("socket is closed");
633 throw new IOException("not connected");
635 return getImpl().getOutputStream();
639 * Sets the TCP_NODELAY option on the socket.
641 * @param on true to enable, false to disable
643 * @exception SocketException If an error occurs or Socket is not connected
647 public void setTcpNoDelay(boolean on) throws SocketException
650 throw new SocketException("socket is closed");
652 getImpl().setOption(SocketOptions.TCP_NODELAY, Boolean.valueOf(on));
656 * Tests whether or not the TCP_NODELAY option is set on the socket.
657 * Returns true if enabled, false if disabled. When on it disables the
658 * Nagle algorithm which means that packets are always send immediatly and
659 * never merged together to reduce network trafic.
661 * @return Whether or not TCP_NODELAY is set
663 * @exception SocketException If an error occurs or Socket not connected
667 public boolean getTcpNoDelay() throws SocketException
670 throw new SocketException("socket is closed");
672 Object on = getImpl().getOption(SocketOptions.TCP_NODELAY);
674 if (on instanceof Boolean)
675 return (((Boolean) on).booleanValue());
677 throw new SocketException("Internal Error");
681 * Sets the value of the SO_LINGER option on the socket. If the
682 * SO_LINGER option is set on a socket and there is still data waiting to
683 * be sent when the socket is closed, then the close operation will block
684 * until either that data is delivered or until the timeout period
685 * expires. The linger interval is specified in hundreths of a second
686 * (platform specific?)
688 * @param on true to enable SO_LINGER, false to disable
689 * @param linger The SO_LINGER timeout in hundreths of a second or -1 if
692 * @exception SocketException If an error occurs or Socket not connected
693 * @exception IllegalArgumentException If linger is negative
697 public void setSoLinger(boolean on, int linger) throws SocketException
700 throw new SocketException("socket is closed");
705 throw new IllegalArgumentException("SO_LINGER must be >= 0");
710 getImpl().setOption(SocketOptions.SO_LINGER, new Integer(linger));
713 getImpl().setOption(SocketOptions.SO_LINGER, Boolean.valueOf(false));
717 * Returns the value of the SO_LINGER option on the socket. If the
718 * SO_LINGER option is set on a socket and there is still data waiting to
719 * be sent when the socket is closed, then the close operation will block
720 * until either that data is delivered or until the timeout period
721 * expires. This method either returns the timeouts (in hundredths of
722 * of a second (platform specific?)) if SO_LINGER is set, or -1 if
723 * SO_LINGER is not set.
725 * @return The SO_LINGER timeout in hundreths of a second or -1
726 * if SO_LINGER not set
728 * @exception SocketException If an error occurs or Socket is not connected
732 public int getSoLinger() throws SocketException
735 throw new SocketException("socket is closed");
737 Object linger = getImpl().getOption(SocketOptions.SO_LINGER);
739 if (linger instanceof Integer)
740 return (((Integer) linger).intValue());
746 * Sends urgent data through the socket
748 * @param data The data to send.
749 * Only the lowest eight bits of data are sent
751 * @exception IOException If an error occurs
755 public void sendUrgentData(int data) throws IOException
758 throw new SocketException("socket is closed");
760 getImpl().sendUrgentData(data);
764 * Enables/disables the SO_OOBINLINE option
766 * @param on True if SO_OOBLINE should be enabled
768 * @exception SocketException If an error occurs
772 public void setOOBInline(boolean on) throws SocketException
775 throw new SocketException("socket is closed");
777 getImpl().setOption(SocketOptions.SO_OOBINLINE, Boolean.valueOf(on));
781 * Returns the current setting of the SO_OOBINLINE option for this socket
783 * @return True if SO_OOBINLINE is set, false otherwise.
785 * @exception SocketException If an error occurs
789 public boolean getOOBInline() throws SocketException
792 throw new SocketException("socket is closed");
794 Object buf = getImpl().getOption(SocketOptions.SO_OOBINLINE);
796 if (buf instanceof Boolean)
797 return (((Boolean) buf).booleanValue());
799 throw new SocketException("Internal Error: Unexpected type");
803 * Sets the value of the SO_TIMEOUT option on the socket. If this value
804 * is set, and an read/write is performed that does not complete within
805 * the timeout period, a short count is returned (or an EWOULDBLOCK signal
806 * would be sent in Unix if no data had been read). A value of 0 for
807 * this option implies that there is no timeout (ie, operations will
808 * block forever). On systems that have separate read and write timeout
809 * values, this method returns the read timeout. This
810 * value is in milliseconds.
812 * @param timeout The length of the timeout in milliseconds, or
813 * 0 to indicate no timeout.
815 * @exception SocketException If an error occurs or Socket not connected
819 public synchronized void setSoTimeout(int timeout) throws SocketException
822 throw new SocketException("socket is closed");
825 throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
827 getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
831 * Returns the value of the SO_TIMEOUT option on the socket. If this value
832 * is set, and an read/write is performed that does not complete within
833 * the timeout period, a short count is returned (or an EWOULDBLOCK signal
834 * would be sent in Unix if no data had been read). A value of 0 for
835 * this option implies that there is no timeout (ie, operations will
836 * block forever). On systems that have separate read and write timeout
837 * values, this method returns the read timeout. This
838 * value is in thousandths of a second (implementation specific?).
840 * @return The length of the timeout in thousandth's of a second or 0
843 * @exception SocketException If an error occurs or Socket not connected
847 public synchronized int getSoTimeout() throws SocketException
850 throw new SocketException("socket is closed");
852 Object timeout = getImpl().getOption(SocketOptions.SO_TIMEOUT);
853 if (timeout instanceof Integer)
854 return (((Integer) timeout).intValue());
860 * This method sets the value for the system level socket option
861 * SO_SNDBUF to the specified value. Note that valid values for this
862 * option are specific to a given operating system.
864 * @param size The new send buffer size.
866 * @exception SocketException If an error occurs or Socket not connected
867 * @exception IllegalArgumentException If size is 0 or negative
871 public void setSendBufferSize(int size) throws SocketException
874 throw new SocketException("socket is closed");
877 throw new IllegalArgumentException("SO_SNDBUF value must be > 0");
879 getImpl().setOption(SocketOptions.SO_SNDBUF, new Integer(size));
883 * This method returns the value of the system level socket option
884 * SO_SNDBUF, which is used by the operating system to tune buffer
885 * sizes for data transfers.
887 * @return The send buffer size.
889 * @exception SocketException If an error occurs or socket not connected
893 public int getSendBufferSize() throws SocketException
896 throw new SocketException("socket is closed");
898 Object buf = getImpl().getOption(SocketOptions.SO_SNDBUF);
900 if (buf instanceof Integer)
901 return (((Integer) buf).intValue());
903 throw new SocketException("Internal Error: Unexpected type");
907 * This method sets the value for the system level socket option
908 * SO_RCVBUF to the specified value. Note that valid values for this
909 * option are specific to a given operating system.
911 * @param size The new receive buffer size.
913 * @exception SocketException If an error occurs or Socket is not connected
914 * @exception IllegalArgumentException If size is 0 or negative
918 public void setReceiveBufferSize(int size) throws SocketException
921 throw new SocketException("socket is closed");
924 throw new IllegalArgumentException("SO_RCVBUF value must be > 0");
926 getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size));
930 * This method returns the value of the system level socket option
931 * SO_RCVBUF, which is used by the operating system to tune buffer
932 * sizes for data transfers.
934 * @return The receive buffer size.
936 * @exception SocketException If an error occurs or Socket is not connected
940 public int getReceiveBufferSize() throws SocketException
943 throw new SocketException("socket is closed");
945 Object buf = getImpl().getOption(SocketOptions.SO_RCVBUF);
947 if (buf instanceof Integer)
948 return (((Integer) buf).intValue());
950 throw new SocketException("Internal Error: Unexpected type");
954 * This method sets the value for the socket level socket option
957 * @param on True if SO_KEEPALIVE should be enabled
959 * @exception SocketException If an error occurs or Socket is not connected
963 public void setKeepAlive(boolean on) throws SocketException
966 throw new SocketException("socket is closed");
968 getImpl().setOption(SocketOptions.SO_KEEPALIVE, Boolean.valueOf(on));
972 * This method returns the value of the socket level socket option
975 * @return The setting
977 * @exception SocketException If an error occurs or Socket is not connected
981 public boolean getKeepAlive() throws SocketException
984 throw new SocketException("socket is closed");
986 Object buf = getImpl().getOption(SocketOptions.SO_KEEPALIVE);
988 if (buf instanceof Boolean)
989 return (((Boolean) buf).booleanValue());
991 throw new SocketException("Internal Error: Unexpected type");
997 * @exception IOException If an error occurs
999 public synchronized void close() throws IOException
1008 if (getChannel() != null)
1009 getChannel().close();
1013 * Converts this <code>Socket</code> to a <code>String</code>.
1015 * @return The <code>String</code> representation of this <code>Socket</code>
1017 public String toString()
1022 return ("Socket[addr=" + getImpl().getInetAddress() + ",port="
1023 + getImpl().getPort() + ",localport="
1024 + getImpl().getLocalPort() + "]");
1026 catch (SocketException e)
1028 // This cannot happen as we are connected.
1031 return "Socket[unconnected]";
1035 * Sets the <code>SocketImplFactory</code>. This may be done only once per
1036 * virtual machine. Subsequent attempts will generate a
1037 * <code>SocketException</code>. Note that a <code>SecurityManager</code>
1038 * check is made prior to setting the factory. If
1039 * insufficient privileges exist to set the factory, then an
1040 * <code>IOException</code> will be thrown.
1042 * @param fac the factory to set
1044 * @exception SecurityException If the <code>SecurityManager</code> does
1045 * not allow this operation.
1046 * @exception SocketException If the SocketImplFactory is already defined
1047 * @exception IOException If any other error occurs
1049 public static synchronized void setSocketImplFactory(SocketImplFactory fac)
1052 // See if already set
1053 if (factory != null)
1054 throw new SocketException("SocketImplFactory already defined");
1056 // Check permissions
1057 SecurityManager sm = System.getSecurityManager();
1059 sm.checkSetFactory();
1062 throw new SocketException("SocketImplFactory cannot be null");
1068 * Closes the input side of the socket stream.
1070 * @exception IOException If an error occurs.
1074 public void shutdownInput() throws IOException
1077 throw new SocketException("socket is closed");
1079 getImpl().shutdownInput();
1080 inputShutdown = true;
1084 * Closes the output side of the socket stream.
1086 * @exception IOException If an error occurs.
1090 public void shutdownOutput() throws IOException
1093 throw new SocketException("socket is closed");
1095 getImpl().shutdownOutput();
1096 outputShutdown = true;
1100 * Returns the socket channel associated with this socket.
1102 * @return the associated socket channel,
1103 * null if no associated channel exists
1107 public SocketChannel getChannel()
1113 * Checks if the SO_REUSEADDR option is enabled
1115 * @return True if SO_REUSEADDR is set, false otherwise.
1117 * @exception SocketException If an error occurs
1121 public boolean getReuseAddress() throws SocketException
1124 throw new SocketException("socket is closed");
1126 Object reuseaddr = getImpl().getOption(SocketOptions.SO_REUSEADDR);
1128 if (! (reuseaddr instanceof Boolean))
1129 throw new SocketException("Internal Error");
1131 return ((Boolean) reuseaddr).booleanValue();
1135 * Enables/Disables the SO_REUSEADDR option
1137 * @param reuseAddress true if SO_REUSEADDR should be enabled,
1140 * @exception SocketException If an error occurs
1144 public void setReuseAddress(boolean reuseAddress) throws SocketException
1147 throw new SocketException("socket is closed");
1149 getImpl().setOption(SocketOptions.SO_REUSEADDR,
1150 Boolean.valueOf(reuseAddress));
1154 * Returns the current traffic class
1156 * @return The current traffic class.
1158 * @exception SocketException If an error occurs
1160 * @see Socket#setTrafficClass(int tc)
1164 public int getTrafficClass() throws SocketException
1167 throw new SocketException("socket is closed");
1169 Object obj = getImpl().getOption(SocketOptions.IP_TOS);
1171 if (obj instanceof Integer)
1172 return ((Integer) obj).intValue();
1174 throw new SocketException("Unexpected type");
1178 * Sets the traffic class value
1180 * @param tc The traffic class
1182 * @exception SocketException If an error occurs
1183 * @exception IllegalArgumentException If tc value is illegal
1185 * @see Socket#getTrafficClass()
1189 public void setTrafficClass(int tc) throws SocketException
1192 throw new SocketException("socket is closed");
1194 if (tc < 0 || tc > 255)
1195 throw new IllegalArgumentException();
1197 getImpl().setOption(SocketOptions.IP_TOS, new Integer(tc));
1201 * Checks if the socket is connected
1203 * @return True if socket is connected, false otherwise.
1207 public boolean isConnected()
1211 if (getImpl() == null)
1214 return getImpl().getInetAddress() != null;
1216 catch (SocketException e)
1223 * Checks if the socket is already bound.
1225 * @return True if socket is bound, false otherwise.
1229 public boolean isBound()
1235 * Checks if the socket is closed.
1237 * @return True if socket is closed, false otherwise.
1241 public boolean isClosed()
1243 return impl == null;
1247 * Checks if the socket's input stream is shutdown
1249 * @return True if input is shut down.
1253 public boolean isInputShutdown()
1255 return inputShutdown;
1259 * Checks if the socket's output stream is shutdown
1261 * @return True if output is shut down.
1265 public boolean isOutputShutdown()
1267 return outputShutdown;