1 /* Socket.java -- Client socket implementation
2 Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
42 import gnu
.java
.net
.PlainSocketImpl
;
43 import java
.io
.InputStream
;
44 import java
.io
.IOException
;
45 import java
.io
.OutputStream
;
46 import java
.nio
.channels
.SocketChannel
;
47 import java
.nio
.channels
.IllegalBlockingModeException
;
49 /* Written using on-line Java Platform 1.2 API Specification.
50 * Status: I believe all methods are implemented.
54 * This class models a client site socket. A socket is a TCP/IP endpoint
55 * for network communications conceptually similar to a file handle.
57 * This class does not actually do any work. Instead, it redirects all of
58 * its calls to a socket implementation object which implements the
59 * <code>SocketImpl</code> interface. The implementation class is
60 * instantiated by factory class that implements the
61 * <code>SocketImplFactory interface</code>. A default
62 * factory is provided, however the factory may be set by a call to
63 * the <code>setSocketImplFactory</code> method. Note that this may only be
64 * done once per virtual machine. If a subsequent attempt is made to set the
65 * factory, a <code>SocketException</code> will be thrown.
67 * @author Aaron M. Renn (arenn@urbanophile.com)
68 * @author Per Bothner (bothner@cygnus.com)
73 * This is the user SocketImplFactory for this class. If this variable is
74 * null, a default factory is used.
76 static SocketImplFactory factory
;
79 * The implementation object to which calls are redirected
81 private SocketImpl impl
;
84 * True if socket implementation was created by calling their create() method.
86 private boolean implCreated
;
89 * True if the socket is bound.
91 private boolean bound
;
94 * True if input is shutdown.
96 private boolean inputShutdown
;
99 * True if output is shutdown.
101 private boolean outputShutdown
;
104 * Initializes a new instance of <code>Socket</code> object without
105 * connecting to a remote host. This useful for subclasses of socket that
106 * might want this behavior.
108 * @specnote This constructor is public since JDK 1.4
114 impl
= factory
.createSocketImpl();
116 impl
= new PlainSocketImpl();
120 * Initializes a new instance of <code>Socket</code> object without
121 * connecting to a remote host. This is useful for subclasses of socket
122 * that might want this behavior.
124 * Additionally, this socket will be created using the supplied
125 * implementation class instead the default class or one returned by a
126 * factory. If this value is <code>null</code>, the default Socket
127 * implementation is used.
129 * @param impl The <code>SocketImpl</code> to use for this
130 * <code>Socket</code>
132 * @exception SocketException If an error occurs
136 protected Socket (SocketImpl impl
) throws SocketException
139 this.impl
= new PlainSocketImpl();
145 * Initializes a new instance of <code>Socket</code> and connects to the
146 * hostname and port specified as arguments.
148 * @param host The name of the host to connect to
149 * @param port The port number to connect to
151 * @exception UnknownHostException If the hostname cannot be resolved to a
153 * @exception IOException If an error occurs
154 * @exception SecurityException If a security manager exists and its
155 * checkConnect method doesn't allow the operation
157 public Socket (String host
, int port
)
158 throws UnknownHostException
, IOException
160 this(InetAddress
.getByName(host
), port
, null, 0, true);
164 * Initializes a new instance of <code>Socket</code> and connects to the
165 * address and port number specified as arguments.
167 * @param address The address to connect to
168 * @param port The port number to connect to
170 * @exception IOException If an error occurs
171 * @exception SecurityException If a security manager exists and its
172 * checkConnect method doesn't allow the operation
174 public Socket (InetAddress address
, int port
)
177 this(address
, port
, null, 0, true);
181 * Initializes a new instance of <code>Socket</code> that connects to the
182 * named host on the specified port and binds to the specified local address
185 * @param host The name of the remote host to connect to.
186 * @param port The remote port to connect to.
187 * @param localAddr The local address to bind to.
188 * @param localPort The local port to bind to.
190 * @exception SecurityException If the <code>SecurityManager</code>
191 * exists and does not allow a connection to the specified host/port or
192 * binding to the specified local host/port.
193 * @exception IOException If a connection error occurs.
197 public Socket (String host
, int port
,
198 InetAddress localAddr
, int localPort
) throws IOException
200 this(InetAddress
.getByName(host
), port
, localAddr
, localPort
, true);
204 * Initializes a new instance of <code>Socket</code> and connects to the
205 * address and port number specified as arguments, plus binds to the
206 * specified local address and port.
208 * @param address The remote address to connect to
209 * @param port The remote port to connect to
210 * @param localAddr The local address to connect to
211 * @param localPort The local port to connect to
213 * @exception IOException If an error occurs
214 * @exception SecurityException If a security manager exists and its
215 * checkConnect method doesn't allow the operation
219 public Socket (InetAddress address
, int port
,
220 InetAddress localAddr
, int localPort
) throws IOException
222 this(address
, port
, localAddr
, localPort
, true);
226 * Initializes a new instance of <code>Socket</code> and connects to the
227 * hostname and port specified as arguments. If the stream argument is set
228 * to <code>true</code>, then a stream socket is created. If it is
229 * <code>false</code>, a datagram socket is created.
231 * @param host The name of the host to connect to
232 * @param port The port to connect to
233 * @param stream <code>true</code> for a stream socket, <code>false</code>
234 * for a datagram socket
236 * @exception IOException If an error occurs
237 * @exception SecurityException If a security manager exists and its
238 * checkConnect method doesn't allow the operation
240 * @deprecated Use the <code>DatagramSocket</code> class to create
241 * datagram oriented sockets.
243 public Socket (String host
, int port
, boolean stream
) throws IOException
245 this(InetAddress
.getByName(host
), port
, null, 0, stream
);
249 * Initializes a new instance of <code>Socket</code> and connects to the
250 * address and port number specified as arguments. If the stream param is
251 * <code>true</code>, a stream socket will be created, otherwise a datagram
254 * @param host The address to connect to
255 * @param port The port number to connect to
256 * @param stream <code>true</code> to create a stream socket,
257 * <code>false</code> to create a datagram socket.
259 * @exception IOException If an error occurs
260 * @exception SecurityException If a security manager exists and its
261 * checkConnect method doesn't allow the operation
263 * @deprecated Use the <code>DatagramSocket</code> class to create
264 * datagram oriented sockets.
266 public Socket (InetAddress host
, int port
, boolean stream
) throws IOException
268 this(host
, port
, null, 0, stream
);
272 * This constructor is where the real work takes place. Connect to the
273 * specified address and port. Use default local values if not specified,
274 * otherwise use the local host and port passed in. Create as stream or
275 * datagram based on "stream" argument.
278 * @param raddr The remote address to connect to
279 * @param rport The remote port to connect to
280 * @param laddr The local address to connect to
281 * @param lport The local port to connect to
282 * @param stream true for a stream socket, false for a datagram socket
284 * @exception IOException If an error occurs
285 * @exception SecurityException If a security manager exists and its
286 * checkConnect method doesn't allow the operation
288 private Socket(InetAddress raddr
, int rport
, InetAddress laddr
, int lport
,
289 boolean stream
) throws IOException
293 SecurityManager sm
= System
.getSecurityManager();
295 sm
.checkConnect(raddr
.getHostName(), rport
);
298 SocketAddress bindaddr
=
299 laddr
== null ?
null : new InetSocketAddress (laddr
, lport
);
303 connect (new InetSocketAddress (raddr
, rport
));
305 // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
306 // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
307 // that default. JDK 1.2 doc infers not to do a bind.
310 // This has to be accessible from java.net.ServerSocket.
312 throws SocketException
322 catch (IOException e
)
324 throw new SocketException(e
.getMessage());
331 * Binds the socket to the givent local address/port
333 * @param bindpoint The address/port to bind to
335 * @exception IOException If an error occurs
336 * @exception SecurityException If a security manager exists and its
337 * checkConnect method doesn't allow the operation
338 * @exception IllegalArgumentException If the address type is not supported
342 public void bind (SocketAddress bindpoint
) throws IOException
345 throw new SocketException("socket is closed");
347 // XXX: JDK 1.4.1 API documentation says that if bindpoint is null the
348 // socket will be bound to an ephemeral port and a valid local address.
349 if (bindpoint
== null)
350 bindpoint
= new InetSocketAddress (InetAddress
.ANY_IF
, 0);
352 if ( !(bindpoint
instanceof InetSocketAddress
))
353 throw new IllegalArgumentException ();
355 InetSocketAddress tmp
= (InetSocketAddress
) bindpoint
;
357 // bind to address/port
360 getImpl().bind (tmp
.getAddress(), tmp
.getPort());
363 catch (IOException exception
)
368 catch (RuntimeException exception
)
381 * Connects the socket with a remote address.
383 * @param endpoint The address to connect to
385 * @exception IOException If an error occurs
386 * @exception IllegalArgumentException If the addess type is not supported
387 * @exception IllegalBlockingModeException If this socket has an associated
388 * channel, and the channel is in non-blocking mode
392 public void connect (SocketAddress endpoint
)
395 connect (endpoint
, 0);
399 * Connects the socket with a remote address. A timeout of zero is
400 * interpreted as an infinite timeout. The connection will then block
401 * until established or an error occurs.
403 * @param endpoint The address to connect to
404 * @param timeout The length of the timeout in milliseconds, or
405 * 0 to indicate no timeout.
407 * @exception IOException If an error occurs
408 * @exception IllegalArgumentException If the address type is not supported
409 * @exception IllegalBlockingModeException If this socket has an associated
410 * channel, and the channel is in non-blocking mode
411 * @exception SocketTimeoutException If the timeout is reached
415 public void connect (SocketAddress endpoint
, int timeout
)
419 throw new SocketException("socket is closed");
421 if (! (endpoint
instanceof InetSocketAddress
))
422 throw new IllegalArgumentException("unsupported address type");
424 // The Sun spec says that if we have an associated channel and
425 // it is in non-blocking mode, we throw an IllegalBlockingModeException.
426 // However, in our implementation if the channel itself initiated this
427 // operation, then we must honor it regardless of its blocking mode.
428 if (getChannel() != null
429 && !getChannel().isBlocking ()
430 && !((PlainSocketImpl
) getImpl()).isInChannelOperation())
431 throw new IllegalBlockingModeException ();
438 getImpl().connect (endpoint
, timeout
);
440 catch (IOException exception
)
445 catch (RuntimeException exception
)
458 * Returns the address of the remote end of the socket. If this socket
459 * is not connected, then <code>null</code> is returned.
461 * @return The remote address this socket is connected to
463 public InetAddress
getInetAddress ()
470 return getImpl().getInetAddress();
472 catch (SocketException e
)
474 // This cannot happen as we are connected.
481 * Returns the local address to which this socket is bound. If this socket
482 * is not connected, then <code>null</code> is returned.
484 * @return The local address
488 public InetAddress
getLocalAddress ()
490 InetAddress addr
= null;
494 addr
= (InetAddress
) getImpl().getOption(SocketOptions
.SO_BINDADDR
);
496 catch(SocketException e
)
498 // (hopefully) shouldn't happen
499 // throw new java.lang.InternalError
500 // ("Error in PlainSocketImpl.getOption");
504 // FIXME: According to libgcj, checkConnect() is supposed to be called
505 // before performing this operation. Problems: 1) We don't have the
506 // addr until after we do it, so we do a post check. 2). The docs I
507 // see don't require this in the Socket case, only DatagramSocket, but
508 // we'll assume they mean both.
509 SecurityManager sm
= System
.getSecurityManager();
511 sm
.checkConnect(addr
.getHostName(), getLocalPort());
517 * Returns the port number of the remote end of the socket connection. If
518 * this socket is not connected, then -1 is returned.
520 * @return The remote port this socket is connected to
522 public int getPort ()
529 if (getImpl() != null)
530 return getImpl().getPort();
532 catch (SocketException e
)
534 // This cannot happen as we are connected.
541 * Returns the local port number to which this socket is bound. If this
542 * socket is not connected, then -1 is returned.
544 * @return The local port
546 public int getLocalPort ()
553 if (getImpl() != null)
554 return getImpl().getLocalPort();
556 catch (SocketException e
)
558 // This cannot happen as we are bound.
565 * If the socket is already bound this returns the local SocketAddress,
570 public SocketAddress
getLocalSocketAddress()
575 InetAddress addr
= getLocalAddress ();
579 return new InetSocketAddress (addr
, getImpl().getLocalPort());
581 catch (SocketException e
)
583 // This cannot happen as we are bound.
589 * If the socket is already connected this returns the remote SocketAddress,
594 public SocketAddress
getRemoteSocketAddress()
601 return new InetSocketAddress (getImpl().getInetAddress (), getImpl().getPort ());
603 catch (SocketException e
)
605 // This cannot happen as we are connected.
611 * Returns an InputStream for reading from this socket.
613 * @return The InputStream object
615 * @exception IOException If an error occurs or Socket is not connected
617 public InputStream
getInputStream () throws IOException
620 throw new SocketException("socket is closed");
623 throw new IOException("not connected");
625 return getImpl().getInputStream();
629 * Returns an OutputStream for writing to this socket.
631 * @return The OutputStream object
633 * @exception IOException If an error occurs or Socket is not connected
635 public OutputStream
getOutputStream () throws IOException
638 throw new SocketException("socket is closed");
641 throw new IOException("not connected");
643 return getImpl().getOutputStream();
647 * Sets the TCP_NODELAY option on the socket.
649 * @param on true to enable, false to disable
651 * @exception SocketException If an error occurs or Socket is not connected
655 public void setTcpNoDelay (boolean on
) throws SocketException
658 throw new SocketException("socket is closed");
660 getImpl().setOption(SocketOptions
.TCP_NODELAY
, Boolean
.valueOf(on
));
664 * Tests whether or not the TCP_NODELAY option is set on the socket.
665 * Returns true if enabled, false if disabled. When on it disables the
666 * Nagle algorithm which means that packets are always send immediatly and
667 * never merged together to reduce network trafic.
669 * @return Whether or not TCP_NODELAY is set
671 * @exception SocketException If an error occurs or Socket not connected
675 public boolean getTcpNoDelay() throws SocketException
678 throw new SocketException("socket is closed");
680 Object on
= getImpl().getOption(SocketOptions
.TCP_NODELAY
);
682 if (on
instanceof Boolean
)
683 return(((Boolean
)on
).booleanValue());
685 throw new SocketException("Internal Error");
689 * Sets the value of the SO_LINGER option on the socket. If the
690 * SO_LINGER option is set on a socket and there is still data waiting to
691 * be sent when the socket is closed, then the close operation will block
692 * until either that data is delivered or until the timeout period
693 * expires. The linger interval is specified in hundreths of a second
694 * (platform specific?)
696 * @param on true to enable SO_LINGER, false to disable
697 * @param linger The SO_LINGER timeout in hundreths of a second or -1 if
700 * @exception SocketException If an error occurs or Socket not connected
701 * @exception IllegalArgumentException If linger is negative
705 public void setSoLinger(boolean on
, int linger
) throws SocketException
708 throw new SocketException("socket is closed");
713 throw new IllegalArgumentException("SO_LINGER must be >= 0");
718 getImpl().setOption(SocketOptions
.SO_LINGER
, new Integer(linger
));
722 getImpl().setOption(SocketOptions
.SO_LINGER
, Boolean
.valueOf(false));
727 * Returns the value of the SO_LINGER option on the socket. If the
728 * SO_LINGER option is set on a socket and there is still data waiting to
729 * be sent when the socket is closed, then the close operation will block
730 * until either that data is delivered or until the timeout period
731 * expires. This method either returns the timeouts (in hundredths of
732 * of a second (platform specific?)) if SO_LINGER is set, or -1 if
733 * SO_LINGER is not set.
735 * @return The SO_LINGER timeout in hundreths of a second or -1
736 * if SO_LINGER not set
738 * @exception SocketException If an error occurs or Socket is not connected
742 public int getSoLinger() throws SocketException
745 throw new SocketException("socket is closed");
747 Object linger
= getImpl().getOption(SocketOptions
.SO_LINGER
);
749 if (linger
instanceof Integer
)
750 return(((Integer
)linger
).intValue());
756 * Sends urgent data through the socket
758 * @param data The data to send.
759 * Only the lowest eight bits of data are sent
761 * @exception IOException If an error occurs
765 public void sendUrgentData (int data
) throws IOException
768 throw new SocketException("socket is closed");
770 getImpl().sendUrgentData (data
);
774 * Enables/disables the SO_OOBINLINE option
776 * @param on True if SO_OOBLINE should be enabled
778 * @exception SocketException If an error occurs
782 public void setOOBInline (boolean on
) throws SocketException
785 throw new SocketException("socket is closed");
787 getImpl().setOption(SocketOptions
.SO_OOBINLINE
, Boolean
.valueOf(on
));
791 * Returns the current setting of the SO_OOBINLINE option for this socket
793 * @return True if SO_OOBINLINE is set, false otherwise.
795 * @exception SocketException If an error occurs
799 public boolean getOOBInline () throws SocketException
802 throw new SocketException("socket is closed");
804 Object buf
= getImpl().getOption(SocketOptions
.SO_OOBINLINE
);
806 if (buf
instanceof Boolean
)
807 return(((Boolean
)buf
).booleanValue());
809 throw new SocketException("Internal Error: Unexpected type");
813 * Sets the value of the SO_TIMEOUT option on the socket. If this value
814 * is set, and an read/write is performed that does not complete within
815 * the timeout period, a short count is returned (or an EWOULDBLOCK signal
816 * would be sent in Unix if no data had been read). A value of 0 for
817 * this option implies that there is no timeout (ie, operations will
818 * block forever). On systems that have separate read and write timeout
819 * values, this method returns the read timeout. This
820 * value is in milliseconds.
822 * @param timeout The length of the timeout in milliseconds, or
823 * 0 to indicate no timeout.
825 * @exception SocketException If an error occurs or Socket not connected
829 public synchronized void setSoTimeout (int timeout
) throws SocketException
832 throw new SocketException("socket is closed");
835 throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
837 getImpl().setOption(SocketOptions
.SO_TIMEOUT
, new Integer(timeout
));
841 * Returns the value of the SO_TIMEOUT option on the socket. If this value
842 * is set, and an read/write is performed that does not complete within
843 * the timeout period, a short count is returned (or an EWOULDBLOCK signal
844 * would be sent in Unix if no data had been read). A value of 0 for
845 * this option implies that there is no timeout (ie, operations will
846 * block forever). On systems that have separate read and write timeout
847 * values, this method returns the read timeout. This
848 * value is in thousandths of a second (implementation specific?).
850 * @return The length of the timeout in thousandth's of a second or 0
853 * @exception SocketException If an error occurs or Socket not connected
857 public synchronized int getSoTimeout () throws SocketException
860 throw new SocketException("socket is closed");
862 Object timeout
= getImpl().getOption(SocketOptions
.SO_TIMEOUT
);
863 if (timeout
instanceof Integer
)
864 return(((Integer
)timeout
).intValue());
870 * This method sets the value for the system level socket option
871 * SO_SNDBUF to the specified value. Note that valid values for this
872 * option are specific to a given operating system.
874 * @param size The new send buffer size.
876 * @exception SocketException If an error occurs or Socket not connected
877 * @exception IllegalArgumentException If size is 0 or negative
881 public void setSendBufferSize (int size
) throws SocketException
884 throw new SocketException("socket is closed");
887 throw new IllegalArgumentException("SO_SNDBUF value must be > 0");
889 getImpl().setOption(SocketOptions
.SO_SNDBUF
, new Integer(size
));
893 * This method returns the value of the system level socket option
894 * SO_SNDBUF, which is used by the operating system to tune buffer
895 * sizes for data transfers.
897 * @return The send buffer size.
899 * @exception SocketException If an error occurs or socket not connected
903 public int getSendBufferSize () throws SocketException
906 throw new SocketException("socket is closed");
908 Object buf
= getImpl().getOption(SocketOptions
.SO_SNDBUF
);
910 if (buf
instanceof Integer
)
911 return(((Integer
)buf
).intValue());
913 throw new SocketException("Internal Error: Unexpected type");
917 * This method sets the value for the system level socket option
918 * SO_RCVBUF to the specified value. Note that valid values for this
919 * option are specific to a given operating system.
921 * @param size The new receive buffer size.
923 * @exception SocketException If an error occurs or Socket is not connected
924 * @exception IllegalArgumentException If size is 0 or negative
928 public void setReceiveBufferSize (int size
) throws SocketException
931 throw new SocketException("socket is closed");
934 throw new IllegalArgumentException("SO_RCVBUF value must be > 0");
936 getImpl().setOption(SocketOptions
.SO_RCVBUF
, new Integer(size
));
940 * This method returns the value of the system level socket option
941 * SO_RCVBUF, which is used by the operating system to tune buffer
942 * sizes for data transfers.
944 * @return The receive buffer size.
946 * @exception SocketException If an error occurs or Socket is not connected
950 public int getReceiveBufferSize () throws SocketException
953 throw new SocketException("socket is closed");
955 Object buf
= getImpl().getOption(SocketOptions
.SO_RCVBUF
);
957 if (buf
instanceof Integer
)
958 return(((Integer
)buf
).intValue());
960 throw new SocketException("Internal Error: Unexpected type");
964 * This method sets the value for the socket level socket option
967 * @param on True if SO_KEEPALIVE should be enabled
969 * @exception SocketException If an error occurs or Socket is not connected
973 public void setKeepAlive (boolean on
) throws SocketException
976 throw new SocketException("socket is closed");
978 getImpl().setOption(SocketOptions
.SO_KEEPALIVE
, Boolean
.valueOf(on
));
982 * This method returns the value of the socket level socket option
985 * @return The setting
987 * @exception SocketException If an error occurs or Socket is not connected
991 public boolean getKeepAlive () throws SocketException
994 throw new SocketException("socket is closed");
996 Object buf
= getImpl().getOption(SocketOptions
.SO_KEEPALIVE
);
998 if (buf
instanceof Boolean
)
999 return(((Boolean
)buf
).booleanValue());
1001 throw new SocketException("Internal Error: Unexpected type");
1005 * Closes the socket.
1007 * @exception IOException If an error occurs
1009 public synchronized void close () throws IOException
1018 if (getChannel() != null)
1019 getChannel().close();
1023 * Converts this <code>Socket</code> to a <code>String</code>.
1025 * @return The <code>String</code> representation of this <code>Socket</code>
1027 public String
toString ()
1032 return ("Socket[addr=" + getImpl().getInetAddress()
1033 + ",port=" + getImpl().getPort()
1034 + ",localport=" + getImpl().getLocalPort()
1037 catch (SocketException e
)
1039 // This cannot happen as we are connected.
1042 return "Socket[unconnected]";
1046 * Sets the <code>SocketImplFactory</code>. This may be done only once per
1047 * virtual machine. Subsequent attempts will generate a
1048 * <code>SocketException</code>. Note that a <code>SecurityManager</code>
1049 * check is made prior to setting the factory. If
1050 * insufficient privileges exist to set the factory, then an
1051 * <code>IOException</code> will be thrown.
1053 * @exception SecurityException If the <code>SecurityManager</code> does
1054 * not allow this operation.
1055 * @exception SocketException If the SocketImplFactory is already defined
1056 * @exception IOException If any other error occurs
1058 public static synchronized void setSocketImplFactory (SocketImplFactory fac
)
1061 // See if already set
1062 if (factory
!= null)
1063 throw new SocketException("SocketImplFactory already defined");
1065 // Check permissions
1066 SecurityManager sm
= System
.getSecurityManager();
1068 sm
.checkSetFactory();
1071 throw new SocketException("SocketImplFactory cannot be null");
1077 * Closes the input side of the socket stream.
1079 * @exception IOException If an error occurs.
1083 public void shutdownInput() throws IOException
1086 throw new SocketException("socket is closed");
1088 getImpl().shutdownInput();
1089 inputShutdown
= true;
1093 * Closes the output side of the socket stream.
1095 * @exception IOException If an error occurs.
1099 public void shutdownOutput() throws IOException
1102 throw new SocketException("socket is closed");
1104 getImpl().shutdownOutput();
1105 outputShutdown
= true;
1109 * Returns the socket channel associated with this socket.
1111 * It returns null if no associated socket exists.
1115 public SocketChannel
getChannel()
1121 * Checks if the SO_REUSEADDR option is enabled
1123 * @return True if SO_REUSEADDR is set, false otherwise.
1125 * @exception SocketException If an error occurs
1129 public boolean getReuseAddress () throws SocketException
1132 throw new SocketException("socket is closed");
1134 Object reuseaddr
= getImpl().getOption (SocketOptions
.SO_REUSEADDR
);
1136 if (!(reuseaddr
instanceof Boolean
))
1137 throw new SocketException ("Internal Error");
1139 return ((Boolean
) reuseaddr
).booleanValue ();
1143 * Enables/Disables the SO_REUSEADDR option
1145 * @param reuseAddress True if SO_REUSEADDR should be set.
1147 * @exception SocketException If an error occurs
1151 public void setReuseAddress (boolean on
) throws SocketException
1153 getImpl().setOption (SocketOptions
.SO_REUSEADDR
, Boolean
.valueOf(on
));
1157 * Returns the current traffic class
1159 * @return The current traffic class.
1161 * @exception SocketException If an error occurs
1163 * @see Socket#setTrafficClass(int tc)
1167 public int getTrafficClass () throws SocketException
1170 throw new SocketException("socket is closed");
1172 Object obj
= getImpl().getOption(SocketOptions
.IP_TOS
);
1174 if (obj
instanceof Integer
)
1175 return ((Integer
) obj
).intValue ();
1177 throw new SocketException ("Unexpected type");
1181 * Sets the traffic class value
1183 * @param tc The traffic class
1185 * @exception SocketException If an error occurs
1186 * @exception IllegalArgumentException If tc value is illegal
1188 * @see Socket#getTrafficClass()
1192 public void setTrafficClass (int tc
) throws SocketException
1195 throw new SocketException("socket is closed");
1197 if (tc
< 0 || tc
> 255)
1198 throw new IllegalArgumentException();
1200 getImpl().setOption (SocketOptions
.IP_TOS
, new Integer (tc
));
1204 * Checks if the socket is connected
1206 * @return True if socket is connected, false otherwise.
1210 public boolean isConnected ()
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
;