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. */
41 import gnu
.java
.net
.PlainSocketImpl
;
42 import java
.io
.IOException
;
43 import java
.io
.InputStream
;
44 import java
.io
.OutputStream
;
45 import java
.nio
.channels
.IllegalBlockingModeException
;
46 import java
.nio
.channels
.SocketChannel
;
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
87 private boolean implCreated
;
90 * True if the socket is bound.
92 private boolean bound
;
95 * True if input is shutdown.
97 private boolean inputShutdown
;
100 * True if output is shutdown.
102 private boolean outputShutdown
;
105 * Initializes a new instance of <code>Socket</code> object without
106 * connecting to a remote host. This useful for subclasses of socket that
107 * might want this behavior.
109 * @specnote This constructor is public since JDK 1.4
115 impl
= factory
.createSocketImpl();
117 impl
= new PlainSocketImpl();
121 * Initializes a new instance of <code>Socket</code> object without
122 * connecting to a remote host. This is useful for subclasses of socket
123 * that might want this behavior.
125 * Additionally, this socket will be created using the supplied
126 * implementation class instead the default class or one returned by a
127 * factory. If this value is <code>null</code>, the default Socket
128 * implementation is used.
130 * @param impl The <code>SocketImpl</code> to use for this
131 * <code>Socket</code>
133 * @exception SocketException If an error occurs
137 protected Socket(SocketImpl impl
) throws SocketException
140 this.impl
= new PlainSocketImpl();
146 * Initializes a new instance of <code>Socket</code> and connects to the
147 * hostname and port specified as arguments.
149 * @param host The name of the host to connect to
150 * @param port The port number to connect to
152 * @exception UnknownHostException If the hostname cannot be resolved to a
154 * @exception IOException If an error occurs
155 * @exception SecurityException If a security manager exists and its
156 * checkConnect method doesn't allow the operation
158 public Socket(String host
, int port
)
159 throws UnknownHostException
, IOException
161 this(InetAddress
.getByName(host
), port
, null, 0, true);
165 * Initializes a new instance of <code>Socket</code> and connects to the
166 * address and port number specified as arguments.
168 * @param address The address to connect to
169 * @param port The port number to connect to
171 * @exception IOException If an error occurs
172 * @exception SecurityException If a security manager exists and its
173 * checkConnect method doesn't allow the operation
175 public Socket(InetAddress address
, int port
) throws IOException
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
, InetAddress localAddr
, int localPort
)
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
, InetAddress localAddr
,
220 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
)
246 this(InetAddress
.getByName(host
), port
, null, 0, stream
);
250 * Initializes a new instance of <code>Socket</code> and connects to the
251 * address and port number specified as arguments. If the stream param is
252 * <code>true</code>, a stream socket will be created, otherwise a datagram
255 * @param host The address to connect to
256 * @param port The port number to connect to
257 * @param stream <code>true</code> to create a stream socket,
258 * <code>false</code> to create a datagram socket.
260 * @exception IOException If an error occurs
261 * @exception SecurityException If a security manager exists and its
262 * checkConnect method doesn't allow the operation
264 * @deprecated Use the <code>DatagramSocket</code> class to create
265 * datagram oriented sockets.
267 public Socket(InetAddress host
, int port
, boolean stream
)
270 this(host
, port
, null, 0, stream
);
274 * This constructor is where the real work takes place. Connect to the
275 * specified address and port. Use default local values if not specified,
276 * otherwise use the local host and port passed in. Create as stream or
277 * datagram based on "stream" argument.
280 * @param raddr The remote address to connect to
281 * @param rport The remote port to connect to
282 * @param laddr The local address to connect to
283 * @param lport The local port to connect to
284 * @param stream true for a stream socket, false for a datagram socket
286 * @exception IOException If an error occurs
287 * @exception SecurityException If a security manager exists and its
288 * checkConnect method doesn't allow the operation
290 private Socket(InetAddress raddr
, int rport
, InetAddress laddr
, int lport
,
291 boolean stream
) throws IOException
295 SecurityManager sm
= System
.getSecurityManager();
297 sm
.checkConnect(raddr
.getHostName(), rport
);
300 SocketAddress bindaddr
=
301 laddr
== null ?
null : new InetSocketAddress(laddr
, lport
);
305 connect(new InetSocketAddress(raddr
, rport
));
307 // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
308 // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
309 // that default. JDK 1.2 doc infers not to do a bind.
312 // This has to be accessible from java.net.ServerSocket.
313 SocketImpl
getImpl() throws SocketException
323 catch (IOException e
)
325 throw new SocketException(e
.getMessage());
332 * Binds the socket to the givent local address/port
334 * @param bindpoint The address/port to bind to
336 * @exception IOException If an error occurs
337 * @exception SecurityException If a security manager exists and its
338 * checkConnect method doesn't allow the operation
339 * @exception IllegalArgumentException If the address type is not supported
343 public void bind(SocketAddress bindpoint
) throws IOException
346 throw new SocketException("socket is closed");
348 // XXX: JDK 1.4.1 API documentation says that if bindpoint is null the
349 // socket will be bound to an ephemeral port and a valid local address.
350 if (bindpoint
== null)
351 bindpoint
= new InetSocketAddress(InetAddress
.ANY_IF
, 0);
353 if (! (bindpoint
instanceof InetSocketAddress
))
354 throw new IllegalArgumentException();
356 InetSocketAddress tmp
= (InetSocketAddress
) bindpoint
;
358 // bind to address/port
361 getImpl().bind(tmp
.getAddress(), tmp
.getPort());
364 catch (IOException exception
)
369 catch (RuntimeException exception
)
382 * Connects the socket with a remote address.
384 * @param endpoint The address to connect to
386 * @exception IOException If an error occurs
387 * @exception IllegalArgumentException If the addess type is not supported
388 * @exception IllegalBlockingModeException If this socket has an associated
389 * channel, and the channel is in non-blocking mode
393 public void connect(SocketAddress endpoint
) throws IOException
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 && ! getChannel().isBlocking()
429 && ! ((PlainSocketImpl
) getImpl()).isInChannelOperation())
430 throw new IllegalBlockingModeException();
437 getImpl().connect(endpoint
, timeout
);
439 catch (IOException exception
)
444 catch (RuntimeException exception
)
457 * Returns the address of the remote end of the socket. If this socket
458 * is not connected, then <code>null</code> is returned.
460 * @return The remote address this socket is connected to
462 public InetAddress
getInetAddress()
469 return getImpl().getInetAddress();
471 catch (SocketException e
)
473 // This cannot happen as we are connected.
480 * Returns the local address to which this socket is bound. If this socket
481 * is not connected, then <code>null</code> is returned.
483 * @return The local address
487 public InetAddress
getLocalAddress()
489 InetAddress addr
= null;
493 addr
= (InetAddress
) getImpl().getOption(SocketOptions
.SO_BINDADDR
);
495 catch (SocketException e
)
497 // (hopefully) shouldn't happen
498 // throw new java.lang.InternalError
499 // ("Error in PlainSocketImpl.getOption");
503 // FIXME: According to libgcj, checkConnect() is supposed to be called
504 // before performing this operation. Problems: 1) We don't have the
505 // addr until after we do it, so we do a post check. 2). The docs I
506 // see don't require this in the Socket case, only DatagramSocket, but
507 // we'll assume they mean both.
508 SecurityManager sm
= System
.getSecurityManager();
510 sm
.checkConnect(addr
.getHostName(), getLocalPort());
516 * Returns the port number of the remote end of the socket connection. If
517 * this socket is not connected, then -1 is returned.
519 * @return The remote port this socket is connected to
528 if (getImpl() != null)
529 return getImpl().getPort();
531 catch (SocketException e
)
533 // This cannot happen as we are connected.
540 * Returns the local port number to which this socket is bound. If this
541 * socket is not connected, then -1 is returned.
543 * @return The local port
545 public int getLocalPort()
552 if (getImpl() != null)
553 return getImpl().getLocalPort();
555 catch (SocketException e
)
557 // This cannot happen as we are bound.
564 * Returns local socket address.
566 * @return the local socket address, null if not bound
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 * Returns the remote socket address.
591 * @return the remote socket address, null of not connected
595 public SocketAddress
getRemoteSocketAddress()
602 return new InetSocketAddress(getImpl().getInetAddress(),
603 getImpl().getPort());
605 catch (SocketException e
)
607 // This cannot happen as we are connected.
613 * Returns an InputStream for reading from this socket.
615 * @return The InputStream object
617 * @exception IOException If an error occurs or Socket is not connected
619 public InputStream
getInputStream() throws IOException
622 throw new SocketException("socket is closed");
625 throw new IOException("not connected");
627 return getImpl().getInputStream();
631 * Returns an OutputStream for writing to this socket.
633 * @return The OutputStream object
635 * @exception IOException If an error occurs or Socket is not connected
637 public OutputStream
getOutputStream() throws IOException
640 throw new SocketException("socket is closed");
643 throw new IOException("not connected");
645 return getImpl().getOutputStream();
649 * Sets the TCP_NODELAY option on the socket.
651 * @param on true to enable, false to disable
653 * @exception SocketException If an error occurs or Socket is not connected
657 public void setTcpNoDelay(boolean on
) throws SocketException
660 throw new SocketException("socket is closed");
662 getImpl().setOption(SocketOptions
.TCP_NODELAY
, Boolean
.valueOf(on
));
666 * Tests whether or not the TCP_NODELAY option is set on the socket.
667 * Returns true if enabled, false if disabled. When on it disables the
668 * Nagle algorithm which means that packets are always send immediatly and
669 * never merged together to reduce network trafic.
671 * @return Whether or not TCP_NODELAY is set
673 * @exception SocketException If an error occurs or Socket not connected
677 public boolean getTcpNoDelay() throws SocketException
680 throw new SocketException("socket is closed");
682 Object on
= getImpl().getOption(SocketOptions
.TCP_NODELAY
);
684 if (on
instanceof Boolean
)
685 return (((Boolean
) on
).booleanValue());
687 throw new SocketException("Internal Error");
691 * Sets the value of the SO_LINGER option on the socket. If the
692 * SO_LINGER option is set on a socket and there is still data waiting to
693 * be sent when the socket is closed, then the close operation will block
694 * until either that data is delivered or until the timeout period
695 * expires. The linger interval is specified in hundreths of a second
696 * (platform specific?)
698 * @param on true to enable SO_LINGER, false to disable
699 * @param linger The SO_LINGER timeout in hundreths of a second or -1 if
702 * @exception SocketException If an error occurs or Socket not connected
703 * @exception IllegalArgumentException If linger is negative
707 public void setSoLinger(boolean on
, int linger
) throws SocketException
710 throw new SocketException("socket is closed");
715 throw new IllegalArgumentException("SO_LINGER must be >= 0");
720 getImpl().setOption(SocketOptions
.SO_LINGER
, new Integer(linger
));
723 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() + ",port="
1033 + getImpl().getPort() + ",localport="
1034 + getImpl().getLocalPort() + "]");
1036 catch (SocketException e
)
1038 // This cannot happen as we are connected.
1041 return "Socket[unconnected]";
1045 * Sets the <code>SocketImplFactory</code>. This may be done only once per
1046 * virtual machine. Subsequent attempts will generate a
1047 * <code>SocketException</code>. Note that a <code>SecurityManager</code>
1048 * check is made prior to setting the factory. If
1049 * insufficient privileges exist to set the factory, then an
1050 * <code>IOException</code> will be thrown.
1052 * @param fac the factory to set
1054 * @exception SecurityException If the <code>SecurityManager</code> does
1055 * not allow this operation.
1056 * @exception SocketException If the SocketImplFactory is already defined
1057 * @exception IOException If any other error occurs
1059 public static synchronized void setSocketImplFactory(SocketImplFactory fac
)
1062 // See if already set
1063 if (factory
!= null)
1064 throw new SocketException("SocketImplFactory already defined");
1066 // Check permissions
1067 SecurityManager sm
= System
.getSecurityManager();
1069 sm
.checkSetFactory();
1072 throw new SocketException("SocketImplFactory cannot be null");
1078 * Closes the input side of the socket stream.
1080 * @exception IOException If an error occurs.
1084 public void shutdownInput() throws IOException
1087 throw new SocketException("socket is closed");
1089 getImpl().shutdownInput();
1090 inputShutdown
= true;
1094 * Closes the output side of the socket stream.
1096 * @exception IOException If an error occurs.
1100 public void shutdownOutput() throws IOException
1103 throw new SocketException("socket is closed");
1105 getImpl().shutdownOutput();
1106 outputShutdown
= true;
1110 * Returns the socket channel associated with this socket.
1112 * @return the associated socket channel,
1113 * null if no associated channel exists
1117 public SocketChannel
getChannel()
1123 * Checks if the SO_REUSEADDR option is enabled
1125 * @return True if SO_REUSEADDR is set, false otherwise.
1127 * @exception SocketException If an error occurs
1131 public boolean getReuseAddress() throws SocketException
1134 throw new SocketException("socket is closed");
1136 Object reuseaddr
= getImpl().getOption(SocketOptions
.SO_REUSEADDR
);
1138 if (! (reuseaddr
instanceof Boolean
))
1139 throw new SocketException("Internal Error");
1141 return ((Boolean
) reuseaddr
).booleanValue();
1145 * Enables/Disables the SO_REUSEADDR option
1147 * @param reuseAddress true if SO_REUSEADDR should be enabled,
1150 * @exception SocketException If an error occurs
1154 public void setReuseAddress(boolean reuseAddress
) throws SocketException
1156 getImpl().setOption(SocketOptions
.SO_REUSEADDR
,
1157 Boolean
.valueOf(reuseAddress
));
1161 * Returns the current traffic class
1163 * @return The current traffic class.
1165 * @exception SocketException If an error occurs
1167 * @see Socket#setTrafficClass(int tc)
1171 public int getTrafficClass() throws SocketException
1174 throw new SocketException("socket is closed");
1176 Object obj
= getImpl().getOption(SocketOptions
.IP_TOS
);
1178 if (obj
instanceof Integer
)
1179 return ((Integer
) obj
).intValue();
1181 throw new SocketException("Unexpected type");
1185 * Sets the traffic class value
1187 * @param tc The traffic class
1189 * @exception SocketException If an error occurs
1190 * @exception IllegalArgumentException If tc value is illegal
1192 * @see Socket#getTrafficClass()
1196 public void setTrafficClass(int tc
) throws SocketException
1199 throw new SocketException("socket is closed");
1201 if (tc
< 0 || tc
> 255)
1202 throw new IllegalArgumentException();
1204 getImpl().setOption(SocketOptions
.IP_TOS
, new Integer(tc
));
1208 * Checks if the socket is connected
1210 * @return True if socket is connected, false otherwise.
1214 public boolean isConnected()
1218 return getImpl().getInetAddress() != null;
1220 catch (SocketException e
)
1227 * Checks if the socket is already bound.
1229 * @return True if socket is bound, false otherwise.
1233 public boolean isBound()
1239 * Checks if the socket is closed.
1241 * @return True if socket is closed, false otherwise.
1245 public boolean isClosed()
1247 return impl
== null;
1251 * Checks if the socket's input stream is shutdown
1253 * @return True if input is shut down.
1257 public boolean isInputShutdown()
1259 return inputShutdown
;
1263 * Checks if the socket's output stream is shutdown
1265 * @return True if output is shut down.
1269 public boolean isOutputShutdown()
1271 return outputShutdown
;