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();
442 getImpl().connect(endpoint, timeout);
444 catch (IOException exception)
449 catch (RuntimeException exception)
462 * Returns the address of the remote end of the socket. If this socket
463 * is not connected, then <code>null</code> is returned.
465 * @return The remote address this socket is connected to
467 public InetAddress getInetAddress()
474 return getImpl().getInetAddress();
476 catch (SocketException e)
478 // This cannot happen as we are connected.
485 * Returns the local address to which this socket is bound. If this socket
486 * is not connected, then a wildcard address, for which
487 * @see InetAddress#isAnyLocalAddress() is <code>true</code>, is returned.
489 * @return The local address
493 public InetAddress getLocalAddress()
496 return InetAddress.ANY_IF;
498 InetAddress addr = null;
502 addr = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR);
504 catch (SocketException e)
506 // (hopefully) shouldn't happen
507 // throw new java.lang.InternalError
508 // ("Error in PlainSocketImpl.getOption");
512 // FIXME: According to libgcj, checkConnect() is supposed to be called
513 // before performing this operation. Problems: 1) We don't have the
514 // addr until after we do it, so we do a post check. 2). The docs I
515 // see don't require this in the Socket case, only DatagramSocket, but
516 // we'll assume they mean both.
517 SecurityManager sm = System.getSecurityManager();
519 sm.checkConnect(addr.getHostName(), getLocalPort());
525 * Returns the port number of the remote end of the socket connection. If
526 * this socket is not connected, then 0 is returned.
528 * @return The remote port this socket is connected to
537 return getImpl().getPort();
539 catch (SocketException e)
541 // This cannot happen as we are connected.
548 * Returns the local port number to which this socket is bound. If this
549 * socket is not connected, then -1 is returned.
551 * @return The local port
553 public int getLocalPort()
560 if (getImpl() != null)
561 return getImpl().getLocalPort();
563 catch (SocketException e)
565 // This cannot happen as we are bound.
572 * Returns local socket address.
574 * @return the local socket address, null if not bound
578 public SocketAddress getLocalSocketAddress()
583 InetAddress addr = getLocalAddress();
587 return new InetSocketAddress(addr, getImpl().getLocalPort());
589 catch (SocketException e)
591 // This cannot happen as we are bound.
597 * Returns the remote socket address.
599 * @return the remote socket address, null of not connected
603 public SocketAddress getRemoteSocketAddress()
610 return new InetSocketAddress(getImpl().getInetAddress(),
611 getImpl().getPort());
613 catch (SocketException e)
615 // This cannot happen as we are connected.
621 * Returns an InputStream for reading from this socket.
623 * @return The InputStream object
625 * @exception IOException If an error occurs or Socket is not connected
627 public InputStream getInputStream() throws IOException
630 throw new SocketException("socket is closed");
633 throw new IOException("not connected");
635 return getImpl().getInputStream();
639 * Returns an OutputStream for writing to this socket.
641 * @return The OutputStream object
643 * @exception IOException If an error occurs or Socket is not connected
645 public OutputStream getOutputStream() throws IOException
648 throw new SocketException("socket is closed");
651 throw new IOException("not connected");
653 return getImpl().getOutputStream();
657 * Sets the TCP_NODELAY option on the socket.
659 * @param on true to enable, false to disable
661 * @exception SocketException If an error occurs or Socket is not connected
665 public void setTcpNoDelay(boolean on) throws SocketException
668 throw new SocketException("socket is closed");
670 getImpl().setOption(SocketOptions.TCP_NODELAY, Boolean.valueOf(on));
674 * Tests whether or not the TCP_NODELAY option is set on the socket.
675 * Returns true if enabled, false if disabled. When on it disables the
676 * Nagle algorithm which means that packets are always send immediatly and
677 * never merged together to reduce network trafic.
679 * @return Whether or not TCP_NODELAY is set
681 * @exception SocketException If an error occurs or Socket not connected
685 public boolean getTcpNoDelay() throws SocketException
688 throw new SocketException("socket is closed");
690 Object on = getImpl().getOption(SocketOptions.TCP_NODELAY);
692 if (on instanceof Boolean)
693 return (((Boolean) on).booleanValue());
695 throw new SocketException("Internal Error");
699 * Sets the value of the SO_LINGER option on the socket. If the
700 * SO_LINGER option is set on a socket and there is still data waiting to
701 * be sent when the socket is closed, then the close operation will block
702 * until either that data is delivered or until the timeout period
703 * expires. The linger interval is specified in hundreths of a second
704 * (platform specific?)
706 * @param on true to enable SO_LINGER, false to disable
707 * @param linger The SO_LINGER timeout in hundreths of a second or -1 if
710 * @exception SocketException If an error occurs or Socket not connected
711 * @exception IllegalArgumentException If linger is negative
715 public void setSoLinger(boolean on, int linger) throws SocketException
718 throw new SocketException("socket is closed");
723 throw new IllegalArgumentException("SO_LINGER must be >= 0");
728 getImpl().setOption(SocketOptions.SO_LINGER, new Integer(linger));
731 getImpl().setOption(SocketOptions.SO_LINGER, Boolean.valueOf(false));
735 * Returns the value of the SO_LINGER option on the socket. If the
736 * SO_LINGER option is set on a socket and there is still data waiting to
737 * be sent when the socket is closed, then the close operation will block
738 * until either that data is delivered or until the timeout period
739 * expires. This method either returns the timeouts (in hundredths of
740 * of a second (platform specific?)) if SO_LINGER is set, or -1 if
741 * SO_LINGER is not set.
743 * @return The SO_LINGER timeout in hundreths of a second or -1
744 * if SO_LINGER not set
746 * @exception SocketException If an error occurs or Socket is not connected
750 public int getSoLinger() throws SocketException
753 throw new SocketException("socket is closed");
755 Object linger = getImpl().getOption(SocketOptions.SO_LINGER);
757 if (linger instanceof Integer)
758 return (((Integer) linger).intValue());
764 * Sends urgent data through the socket
766 * @param data The data to send.
767 * Only the lowest eight bits of data are sent
769 * @exception IOException If an error occurs
773 public void sendUrgentData(int data) throws IOException
776 throw new SocketException("socket is closed");
778 getImpl().sendUrgentData(data);
782 * Enables/disables the SO_OOBINLINE option
784 * @param on True if SO_OOBLINE should be enabled
786 * @exception SocketException If an error occurs
790 public void setOOBInline(boolean on) throws SocketException
793 throw new SocketException("socket is closed");
795 getImpl().setOption(SocketOptions.SO_OOBINLINE, Boolean.valueOf(on));
799 * Returns the current setting of the SO_OOBINLINE option for this socket
801 * @return True if SO_OOBINLINE is set, false otherwise.
803 * @exception SocketException If an error occurs
807 public boolean getOOBInline() throws SocketException
810 throw new SocketException("socket is closed");
812 Object buf = getImpl().getOption(SocketOptions.SO_OOBINLINE);
814 if (buf instanceof Boolean)
815 return (((Boolean) buf).booleanValue());
817 throw new SocketException("Internal Error: Unexpected type");
821 * Sets the value of the SO_TIMEOUT option on the socket. If this value
822 * is set, and an read/write is performed that does not complete within
823 * the timeout period, a short count is returned (or an EWOULDBLOCK signal
824 * would be sent in Unix if no data had been read). A value of 0 for
825 * this option implies that there is no timeout (ie, operations will
826 * block forever). On systems that have separate read and write timeout
827 * values, this method returns the read timeout. This
828 * value is in milliseconds.
830 * @param timeout The length of the timeout in milliseconds, or
831 * 0 to indicate no timeout.
833 * @exception SocketException If an error occurs or Socket not connected
837 public synchronized void setSoTimeout(int timeout) throws SocketException
840 throw new SocketException("socket is closed");
843 throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
845 getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
849 * Returns the value of the SO_TIMEOUT option on the socket. If this value
850 * is set, and an read/write is performed that does not complete within
851 * the timeout period, a short count is returned (or an EWOULDBLOCK signal
852 * would be sent in Unix if no data had been read). A value of 0 for
853 * this option implies that there is no timeout (ie, operations will
854 * block forever). On systems that have separate read and write timeout
855 * values, this method returns the read timeout. This
856 * value is in thousandths of a second (implementation specific?).
858 * @return The length of the timeout in thousandth's of a second or 0
861 * @exception SocketException If an error occurs or Socket not connected
865 public synchronized int getSoTimeout() throws SocketException
868 throw new SocketException("socket is closed");
870 Object timeout = getImpl().getOption(SocketOptions.SO_TIMEOUT);
871 if (timeout instanceof Integer)
872 return (((Integer) timeout).intValue());
878 * This method sets the value for the system level socket option
879 * SO_SNDBUF to the specified value. Note that valid values for this
880 * option are specific to a given operating system.
882 * @param size The new send buffer size.
884 * @exception SocketException If an error occurs or Socket not connected
885 * @exception IllegalArgumentException If size is 0 or negative
889 public void setSendBufferSize(int size) throws SocketException
892 throw new SocketException("socket is closed");
895 throw new IllegalArgumentException("SO_SNDBUF value must be > 0");
897 getImpl().setOption(SocketOptions.SO_SNDBUF, new Integer(size));
901 * This method returns the value of the system level socket option
902 * SO_SNDBUF, which is used by the operating system to tune buffer
903 * sizes for data transfers.
905 * @return The send buffer size.
907 * @exception SocketException If an error occurs or socket not connected
911 public int getSendBufferSize() throws SocketException
914 throw new SocketException("socket is closed");
916 Object buf = getImpl().getOption(SocketOptions.SO_SNDBUF);
918 if (buf instanceof Integer)
919 return (((Integer) buf).intValue());
921 throw new SocketException("Internal Error: Unexpected type");
925 * This method sets the value for the system level socket option
926 * SO_RCVBUF to the specified value. Note that valid values for this
927 * option are specific to a given operating system.
929 * @param size The new receive buffer size.
931 * @exception SocketException If an error occurs or Socket is not connected
932 * @exception IllegalArgumentException If size is 0 or negative
936 public void setReceiveBufferSize(int size) throws SocketException
939 throw new SocketException("socket is closed");
942 throw new IllegalArgumentException("SO_RCVBUF value must be > 0");
944 getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size));
948 * This method returns the value of the system level socket option
949 * SO_RCVBUF, which is used by the operating system to tune buffer
950 * sizes for data transfers.
952 * @return The receive buffer size.
954 * @exception SocketException If an error occurs or Socket is not connected
958 public int getReceiveBufferSize() throws SocketException
961 throw new SocketException("socket is closed");
963 Object buf = getImpl().getOption(SocketOptions.SO_RCVBUF);
965 if (buf instanceof Integer)
966 return (((Integer) buf).intValue());
968 throw new SocketException("Internal Error: Unexpected type");
972 * This method sets the value for the socket level socket option
975 * @param on True if SO_KEEPALIVE should be enabled
977 * @exception SocketException If an error occurs or Socket is not connected
981 public void setKeepAlive(boolean on) throws SocketException
984 throw new SocketException("socket is closed");
986 getImpl().setOption(SocketOptions.SO_KEEPALIVE, Boolean.valueOf(on));
990 * This method returns the value of the socket level socket option
993 * @return The setting
995 * @exception SocketException If an error occurs or Socket is not connected
999 public boolean getKeepAlive() throws SocketException
1002 throw new SocketException("socket is closed");
1004 Object buf = getImpl().getOption(SocketOptions.SO_KEEPALIVE);
1006 if (buf instanceof Boolean)
1007 return (((Boolean) buf).booleanValue());
1009 throw new SocketException("Internal Error: Unexpected type");
1013 * Closes the socket.
1015 * @exception IOException If an error occurs
1017 public synchronized void close() throws IOException
1026 if (getChannel() != null)
1027 getChannel().close();
1031 * Converts this <code>Socket</code> to a <code>String</code>.
1033 * @return The <code>String</code> representation of this <code>Socket</code>
1035 public String toString()
1040 return ("Socket[addr=" + getImpl().getInetAddress() + ",port="
1041 + getImpl().getPort() + ",localport="
1042 + getImpl().getLocalPort() + "]");
1044 catch (SocketException e)
1046 // This cannot happen as we are connected.
1049 return "Socket[unconnected]";
1053 * Sets the <code>SocketImplFactory</code>. This may be done only once per
1054 * virtual machine. Subsequent attempts will generate a
1055 * <code>SocketException</code>. Note that a <code>SecurityManager</code>
1056 * check is made prior to setting the factory. If
1057 * insufficient privileges exist to set the factory, then an
1058 * <code>IOException</code> will be thrown.
1060 * @param fac the factory to set
1062 * @exception SecurityException If the <code>SecurityManager</code> does
1063 * not allow this operation.
1064 * @exception SocketException If the SocketImplFactory is already defined
1065 * @exception IOException If any other error occurs
1067 public static synchronized void setSocketImplFactory(SocketImplFactory fac)
1070 // See if already set
1071 if (factory != null)
1072 throw new SocketException("SocketImplFactory already defined");
1074 // Check permissions
1075 SecurityManager sm = System.getSecurityManager();
1077 sm.checkSetFactory();
1080 throw new SocketException("SocketImplFactory cannot be null");
1086 * Closes the input side of the socket stream.
1088 * @exception IOException If an error occurs.
1092 public void shutdownInput() throws IOException
1095 throw new SocketException("socket is closed");
1097 getImpl().shutdownInput();
1098 inputShutdown = true;
1102 * Closes the output side of the socket stream.
1104 * @exception IOException If an error occurs.
1108 public void shutdownOutput() throws IOException
1111 throw new SocketException("socket is closed");
1113 getImpl().shutdownOutput();
1114 outputShutdown = true;
1118 * Returns the socket channel associated with this socket.
1120 * @return the associated socket channel,
1121 * null if no associated channel exists
1125 public SocketChannel getChannel()
1131 * Checks if the SO_REUSEADDR option is enabled
1133 * @return True if SO_REUSEADDR is set, false otherwise.
1135 * @exception SocketException If an error occurs
1139 public boolean getReuseAddress() throws SocketException
1142 throw new SocketException("socket is closed");
1144 Object reuseaddr = getImpl().getOption(SocketOptions.SO_REUSEADDR);
1146 if (! (reuseaddr instanceof Boolean))
1147 throw new SocketException("Internal Error");
1149 return ((Boolean) reuseaddr).booleanValue();
1153 * Enables/Disables the SO_REUSEADDR option
1155 * @param reuseAddress true if SO_REUSEADDR should be enabled,
1158 * @exception SocketException If an error occurs
1162 public void setReuseAddress(boolean reuseAddress) throws SocketException
1165 throw new SocketException("socket is closed");
1167 getImpl().setOption(SocketOptions.SO_REUSEADDR,
1168 Boolean.valueOf(reuseAddress));
1172 * Returns the current traffic class
1174 * @return The current traffic class.
1176 * @exception SocketException If an error occurs
1178 * @see Socket#setTrafficClass(int tc)
1182 public int getTrafficClass() throws SocketException
1185 throw new SocketException("socket is closed");
1187 Object obj = getImpl().getOption(SocketOptions.IP_TOS);
1189 if (obj instanceof Integer)
1190 return ((Integer) obj).intValue();
1192 throw new SocketException("Unexpected type");
1196 * Sets the traffic class value
1198 * @param tc The traffic class
1200 * @exception SocketException If an error occurs
1201 * @exception IllegalArgumentException If tc value is illegal
1203 * @see Socket#getTrafficClass()
1207 public void setTrafficClass(int tc) throws SocketException
1210 throw new SocketException("socket is closed");
1212 if (tc < 0 || tc > 255)
1213 throw new IllegalArgumentException();
1215 getImpl().setOption(SocketOptions.IP_TOS, new Integer(tc));
1219 * Checks if the socket is connected
1221 * @return True if socket is connected, false otherwise.
1225 public boolean isConnected()
1229 if (getImpl() == null)
1232 return getImpl().getInetAddress() != null;
1234 catch (SocketException e)
1241 * Checks if the socket is already bound.
1243 * @return True if socket is bound, false otherwise.
1247 public boolean isBound()
1253 * Checks if the socket is closed.
1255 * @return True if socket is closed, false otherwise.
1259 public boolean isClosed()
1261 return impl == null;
1265 * Checks if the socket's input stream is shutdown
1267 * @return True if input is shut down.
1271 public boolean isInputShutdown()
1273 return inputShutdown;
1277 * Checks if the socket's output stream is shutdown
1279 * @return True if output is shut down.
1283 public boolean isOutputShutdown()
1285 return outputShutdown;