33 import java.security.PrivilegedExceptionAction;
34 import java.security.PrivilegedAction;
35 import java.util.Set;
36 import java.util.Collections;
37
38 /**
39 * This class implements client sockets (also called just
40 * "sockets"). A socket is an endpoint for communication
41 * between two machines.
42 * <p>
43 * The actual work of the socket is performed by an instance of the
44 * {@code SocketImpl} class. An application, by changing
45 * the socket factory that creates the socket implementation,
46 * can configure itself to create sockets appropriate to the local
47 * firewall.
48 *
49 * @author unascribed
50 * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
51 * @see java.net.SocketImpl
52 * @see java.nio.channels.SocketChannel
53 * @since JDK1.0
54 */
55 public
56 class Socket implements java.io.Closeable {
57 /**
58 * Various states of this socket.
59 */
60 private boolean created = false;
61 private boolean bound = false;
62 private boolean connected = false;
63 private boolean closed = false;
64 private Object closeLock = new Object();
65 private boolean shutIn = false;
66 private boolean shutOut = false;
67
68 /**
69 * The implementation of this Socket.
70 */
71 SocketImpl impl;
72
73 /**
74 * Are we using an older SocketImpl?
75 */
76 private boolean oldImpl = false;
77
78 /**
79 * Creates an unconnected socket, with the
80 * system-default type of SocketImpl.
81 *
82 * @since JDK1.1
83 * @revised 1.4
84 */
85 public Socket() {
86 setImpl();
87 }
88
89 /**
90 * Creates an unconnected socket, specifying the type of proxy, if any,
91 * that should be used regardless of any other settings.
92 * <P>
93 * If there is a security manager, its {@code checkConnect} method
94 * is called with the proxy host address and port number
95 * as its arguments. This could result in a SecurityException.
96 * <P>
97 * Examples:
98 * <UL> <LI>{@code Socket s = new Socket(Proxy.NO_PROXY);} will create
99 * a plain socket ignoring any other proxy configuration.</LI>
100 * <LI>{@code Socket s = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("socks.mydom.com", 1080)));}
101 * will create a socket connecting through the specified SOCKS proxy
102 * server.</LI>
144 if (p == Proxy.NO_PROXY) {
145 if (factory == null) {
146 impl = new PlainSocketImpl();
147 impl.setSocket(this);
148 } else
149 setImpl();
150 } else
151 throw new IllegalArgumentException("Invalid Proxy");
152 }
153 }
154
155 /**
156 * Creates an unconnected Socket with a user-specified
157 * SocketImpl.
158 *
159 * @param impl an instance of a <B>SocketImpl</B>
160 * the subclass wishes to use on the Socket.
161 *
162 * @exception SocketException if there is an error in the underlying protocol,
163 * such as a TCP error.
164 * @since JDK1.1
165 */
166 protected Socket(SocketImpl impl) throws SocketException {
167 this.impl = impl;
168 if (impl != null) {
169 checkOldImpl();
170 this.impl.setSocket(this);
171 }
172 }
173
174 /**
175 * Creates a stream socket and connects it to the specified port
176 * number on the named host.
177 * <p>
178 * If the specified host is {@code null} it is the equivalent of
179 * specifying the address as
180 * {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}.
181 * In other words, it is equivalent to specifying an address of the
182 * loopback interface. </p>
183 * <p>
184 * If the application has specified a server socket factory, that
264 * If there is a security manager, its
265 * {@code checkConnect} method is called
266 * with the host address and {@code port}
267 * as its arguments. This could result in a SecurityException.
268 *
269 * @param host the name of the remote host, or {@code null} for the loopback address.
270 * @param port the remote port
271 * @param localAddr the local address the socket is bound to, or
272 * {@code null} for the {@code anyLocal} address.
273 * @param localPort the local port the socket is bound to, or
274 * {@code zero} for a system selected free port.
275 * @exception IOException if an I/O error occurs when creating the socket.
276 * @exception SecurityException if a security manager exists and its
277 * {@code checkConnect} method doesn't allow the connection
278 * to the destination, or if its {@code checkListen} method
279 * doesn't allow the bind to the local port.
280 * @exception IllegalArgumentException if the port parameter or localPort
281 * parameter is outside the specified range of valid port values,
282 * which is between 0 and 65535, inclusive.
283 * @see SecurityManager#checkConnect
284 * @since JDK1.1
285 */
286 public Socket(String host, int port, InetAddress localAddr,
287 int localPort) throws IOException {
288 this(host != null ? new InetSocketAddress(host, port) :
289 new InetSocketAddress(InetAddress.getByName(null), port),
290 new InetSocketAddress(localAddr, localPort), true);
291 }
292
293 /**
294 * Creates a socket and connects it to the specified remote address on
295 * the specified remote port. The Socket will also bind() to the local
296 * address and port supplied.
297 * <p>
298 * If the specified local address is {@code null} it is the equivalent of
299 * specifying the address as the AnyLocal address
300 * (see {@link java.net.InetAddress#isAnyLocalAddress InetAddress.isAnyLocalAddress}{@code ()}).
301 * <p>
302 * A local port number of {@code zero} will let the system pick up a
303 * free port in the {@code bind} operation.</p>
304 * <p>
306 * {@code checkConnect} method is called
307 * with the host address and {@code port}
308 * as its arguments. This could result in a SecurityException.
309 *
310 * @param address the remote address
311 * @param port the remote port
312 * @param localAddr the local address the socket is bound to, or
313 * {@code null} for the {@code anyLocal} address.
314 * @param localPort the local port the socket is bound to or
315 * {@code zero} for a system selected free port.
316 * @exception IOException if an I/O error occurs when creating the socket.
317 * @exception SecurityException if a security manager exists and its
318 * {@code checkConnect} method doesn't allow the connection
319 * to the destination, or if its {@code checkListen} method
320 * doesn't allow the bind to the local port.
321 * @exception IllegalArgumentException if the port parameter or localPort
322 * parameter is outside the specified range of valid port values,
323 * which is between 0 and 65535, inclusive.
324 * @exception NullPointerException if {@code address} is null.
325 * @see SecurityManager#checkConnect
326 * @since JDK1.1
327 */
328 public Socket(InetAddress address, int port, InetAddress localAddr,
329 int localPort) throws IOException {
330 this(address != null ? new InetSocketAddress(address, port) : null,
331 new InetSocketAddress(localAddr, localPort), true);
332 }
333
334 /**
335 * Creates a stream socket and connects it to the specified port
336 * number on the named host.
337 * <p>
338 * If the specified host is {@code null} it is the equivalent of
339 * specifying the address as
340 * {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}.
341 * In other words, it is equivalent to specifying an address of the
342 * loopback interface. </p>
343 * <p>
344 * If the stream argument is {@code true}, this creates a
345 * stream socket. If the stream argument is {@code false}, it
346 * creates a datagram socket.
691 if (!isConnected())
692 return null;
693 try {
694 return getImpl().getInetAddress();
695 } catch (SocketException e) {
696 }
697 return null;
698 }
699
700 /**
701 * Gets the local address to which the socket is bound.
702 * <p>
703 * If there is a security manager set, its {@code checkConnect} method is
704 * called with the local address and {@code -1} as its arguments to see
705 * if the operation is allowed. If the operation is not allowed,
706 * the {@link InetAddress#getLoopbackAddress loopback} address is returned.
707 *
708 * @return the local address to which the socket is bound,
709 * the loopback address if denied by the security manager, or
710 * the wildcard address if the socket is closed or not bound yet.
711 * @since JDK1.1
712 *
713 * @see SecurityManager#checkConnect
714 */
715 public InetAddress getLocalAddress() {
716 // This is for backward compatibility
717 if (!isBound())
718 return InetAddress.anyLocalAddress();
719 InetAddress in = null;
720 try {
721 in = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR);
722 SecurityManager sm = System.getSecurityManager();
723 if (sm != null)
724 sm.checkConnect(in.getHostAddress(), -1);
725 if (in.isAnyLocalAddress()) {
726 in = InetAddress.anyLocalAddress();
727 }
728 } catch (SecurityException e) {
729 in = InetAddress.getLoopbackAddress();
730 } catch (Exception e) {
731 in = InetAddress.anyLocalAddress(); // "0.0.0.0"
955 public OutputStream run() throws IOException {
956 return impl.getOutputStream();
957 }
958 });
959 } catch (java.security.PrivilegedActionException e) {
960 throw (IOException) e.getException();
961 }
962 return os;
963 }
964
965 /**
966 * Enable/disable {@link SocketOptions#TCP_NODELAY TCP_NODELAY}
967 * (disable/enable Nagle's algorithm).
968 *
969 * @param on {@code true} to enable TCP_NODELAY,
970 * {@code false} to disable.
971 *
972 * @exception SocketException if there is an error
973 * in the underlying protocol, such as a TCP error.
974 *
975 * @since JDK1.1
976 *
977 * @see #getTcpNoDelay()
978 */
979 public void setTcpNoDelay(boolean on) throws SocketException {
980 if (isClosed())
981 throw new SocketException("Socket is closed");
982 getImpl().setOption(SocketOptions.TCP_NODELAY, Boolean.valueOf(on));
983 }
984
985 /**
986 * Tests if {@link SocketOptions#TCP_NODELAY TCP_NODELAY} is enabled.
987 *
988 * @return a {@code boolean} indicating whether or not
989 * {@link SocketOptions#TCP_NODELAY TCP_NODELAY} is enabled.
990 * @exception SocketException if there is an error
991 * in the underlying protocol, such as a TCP error.
992 * @since JDK1.1
993 * @see #setTcpNoDelay(boolean)
994 */
995 public boolean getTcpNoDelay() throws SocketException {
996 if (isClosed())
997 throw new SocketException("Socket is closed");
998 return ((Boolean) getImpl().getOption(SocketOptions.TCP_NODELAY)).booleanValue();
999 }
1000
1001 /**
1002 * Enable/disable {@link SocketOptions#SO_LINGER SO_LINGER} with the
1003 * specified linger time in seconds. The maximum timeout value is platform
1004 * specific.
1005 *
1006 * The setting only affects socket close.
1007 *
1008 * @param on whether or not to linger on.
1009 * @param linger how long to linger for, if on is true.
1010 * @exception SocketException if there is an error
1011 * in the underlying protocol, such as a TCP error.
1012 * @exception IllegalArgumentException if the linger value is negative.
1013 * @since JDK1.1
1014 * @see #getSoLinger()
1015 */
1016 public void setSoLinger(boolean on, int linger) throws SocketException {
1017 if (isClosed())
1018 throw new SocketException("Socket is closed");
1019 if (!on) {
1020 getImpl().setOption(SocketOptions.SO_LINGER, on);
1021 } else {
1022 if (linger < 0) {
1023 throw new IllegalArgumentException("invalid value for SO_LINGER");
1024 }
1025 if (linger > 65535)
1026 linger = 65535;
1027 getImpl().setOption(SocketOptions.SO_LINGER, new Integer(linger));
1028 }
1029 }
1030
1031 /**
1032 * Returns setting for {@link SocketOptions#SO_LINGER SO_LINGER}.
1033 * -1 returns implies that the
1034 * option is disabled.
1035 *
1036 * The setting only affects socket close.
1037 *
1038 * @return the setting for {@link SocketOptions#SO_LINGER SO_LINGER}.
1039 * @exception SocketException if there is an error
1040 * in the underlying protocol, such as a TCP error.
1041 * @since JDK1.1
1042 * @see #setSoLinger(boolean, int)
1043 */
1044 public int getSoLinger() throws SocketException {
1045 if (isClosed())
1046 throw new SocketException("Socket is closed");
1047 Object o = getImpl().getOption(SocketOptions.SO_LINGER);
1048 if (o instanceof Integer) {
1049 return ((Integer) o).intValue();
1050 } else {
1051 return -1;
1052 }
1053 }
1054
1055 /**
1056 * Send one byte of urgent data on the socket. The byte to be sent is the lowest eight
1057 * bits of the data parameter. The urgent byte is
1058 * sent after any preceding writes to the socket OutputStream
1059 * and before any future writes to the OutputStream.
1060 * @param data The byte of data to send
1061 * @exception IOException if there is an error
1114 public boolean getOOBInline() throws SocketException {
1115 if (isClosed())
1116 throw new SocketException("Socket is closed");
1117 return ((Boolean) getImpl().getOption(SocketOptions.SO_OOBINLINE)).booleanValue();
1118 }
1119
1120 /**
1121 * Enable/disable {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}
1122 * with the specified timeout, in milliseconds. With this option set
1123 * to a non-zero timeout, a read() call on the InputStream associated with
1124 * this Socket will block for only this amount of time. If the timeout
1125 * expires, a <B>java.net.SocketTimeoutException</B> is raised, though the
1126 * Socket is still valid. The option <B>must</B> be enabled
1127 * prior to entering the blocking operation to have effect. The
1128 * timeout must be {@code > 0}.
1129 * A timeout of zero is interpreted as an infinite timeout.
1130 *
1131 * @param timeout the specified timeout, in milliseconds.
1132 * @exception SocketException if there is an error
1133 * in the underlying protocol, such as a TCP error.
1134 * @since JDK 1.1
1135 * @see #getSoTimeout()
1136 */
1137 public synchronized void setSoTimeout(int timeout) throws SocketException {
1138 if (isClosed())
1139 throw new SocketException("Socket is closed");
1140 if (timeout < 0)
1141 throw new IllegalArgumentException("timeout can't be negative");
1142
1143 getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
1144 }
1145
1146 /**
1147 * Returns setting for {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}.
1148 * 0 returns implies that the option is disabled (i.e., timeout of infinity).
1149 *
1150 * @return the setting for {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}
1151 * @exception SocketException if there is an error
1152 * in the underlying protocol, such as a TCP error.
1153 *
1154 * @since JDK1.1
1155 * @see #setSoTimeout(int)
1156 */
1157 public synchronized int getSoTimeout() throws SocketException {
1158 if (isClosed())
1159 throw new SocketException("Socket is closed");
1160 Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT);
1161 /* extra type safety */
1162 if (o instanceof Integer) {
1163 return ((Integer) o).intValue();
1164 } else {
1165 return 0;
1166 }
1167 }
1168
1169 /**
1170 * Sets the {@link SocketOptions#SO_SNDBUF SO_SNDBUF} option to the
1171 * specified value for this {@code Socket}.
1172 * The {@link SocketOptions#SO_SNDBUF SO_SNDBUF} option is used by the
1173 * platform's networking code as a hint for the size to set the underlying
1174 * network I/O buffers.
|
33 import java.security.PrivilegedExceptionAction;
34 import java.security.PrivilegedAction;
35 import java.util.Set;
36 import java.util.Collections;
37
38 /**
39 * This class implements client sockets (also called just
40 * "sockets"). A socket is an endpoint for communication
41 * between two machines.
42 * <p>
43 * The actual work of the socket is performed by an instance of the
44 * {@code SocketImpl} class. An application, by changing
45 * the socket factory that creates the socket implementation,
46 * can configure itself to create sockets appropriate to the local
47 * firewall.
48 *
49 * @author unascribed
50 * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
51 * @see java.net.SocketImpl
52 * @see java.nio.channels.SocketChannel
53 * @since 1.0
54 */
55 public
56 class Socket implements java.io.Closeable {
57 /**
58 * Various states of this socket.
59 */
60 private boolean created = false;
61 private boolean bound = false;
62 private boolean connected = false;
63 private boolean closed = false;
64 private Object closeLock = new Object();
65 private boolean shutIn = false;
66 private boolean shutOut = false;
67
68 /**
69 * The implementation of this Socket.
70 */
71 SocketImpl impl;
72
73 /**
74 * Are we using an older SocketImpl?
75 */
76 private boolean oldImpl = false;
77
78 /**
79 * Creates an unconnected socket, with the
80 * system-default type of SocketImpl.
81 *
82 * @since 1.1
83 * @revised 1.4
84 */
85 public Socket() {
86 setImpl();
87 }
88
89 /**
90 * Creates an unconnected socket, specifying the type of proxy, if any,
91 * that should be used regardless of any other settings.
92 * <P>
93 * If there is a security manager, its {@code checkConnect} method
94 * is called with the proxy host address and port number
95 * as its arguments. This could result in a SecurityException.
96 * <P>
97 * Examples:
98 * <UL> <LI>{@code Socket s = new Socket(Proxy.NO_PROXY);} will create
99 * a plain socket ignoring any other proxy configuration.</LI>
100 * <LI>{@code Socket s = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("socks.mydom.com", 1080)));}
101 * will create a socket connecting through the specified SOCKS proxy
102 * server.</LI>
144 if (p == Proxy.NO_PROXY) {
145 if (factory == null) {
146 impl = new PlainSocketImpl();
147 impl.setSocket(this);
148 } else
149 setImpl();
150 } else
151 throw new IllegalArgumentException("Invalid Proxy");
152 }
153 }
154
155 /**
156 * Creates an unconnected Socket with a user-specified
157 * SocketImpl.
158 *
159 * @param impl an instance of a <B>SocketImpl</B>
160 * the subclass wishes to use on the Socket.
161 *
162 * @exception SocketException if there is an error in the underlying protocol,
163 * such as a TCP error.
164 * @since 1.1
165 */
166 protected Socket(SocketImpl impl) throws SocketException {
167 this.impl = impl;
168 if (impl != null) {
169 checkOldImpl();
170 this.impl.setSocket(this);
171 }
172 }
173
174 /**
175 * Creates a stream socket and connects it to the specified port
176 * number on the named host.
177 * <p>
178 * If the specified host is {@code null} it is the equivalent of
179 * specifying the address as
180 * {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}.
181 * In other words, it is equivalent to specifying an address of the
182 * loopback interface. </p>
183 * <p>
184 * If the application has specified a server socket factory, that
264 * If there is a security manager, its
265 * {@code checkConnect} method is called
266 * with the host address and {@code port}
267 * as its arguments. This could result in a SecurityException.
268 *
269 * @param host the name of the remote host, or {@code null} for the loopback address.
270 * @param port the remote port
271 * @param localAddr the local address the socket is bound to, or
272 * {@code null} for the {@code anyLocal} address.
273 * @param localPort the local port the socket is bound to, or
274 * {@code zero} for a system selected free port.
275 * @exception IOException if an I/O error occurs when creating the socket.
276 * @exception SecurityException if a security manager exists and its
277 * {@code checkConnect} method doesn't allow the connection
278 * to the destination, or if its {@code checkListen} method
279 * doesn't allow the bind to the local port.
280 * @exception IllegalArgumentException if the port parameter or localPort
281 * parameter is outside the specified range of valid port values,
282 * which is between 0 and 65535, inclusive.
283 * @see SecurityManager#checkConnect
284 * @since 1.1
285 */
286 public Socket(String host, int port, InetAddress localAddr,
287 int localPort) throws IOException {
288 this(host != null ? new InetSocketAddress(host, port) :
289 new InetSocketAddress(InetAddress.getByName(null), port),
290 new InetSocketAddress(localAddr, localPort), true);
291 }
292
293 /**
294 * Creates a socket and connects it to the specified remote address on
295 * the specified remote port. The Socket will also bind() to the local
296 * address and port supplied.
297 * <p>
298 * If the specified local address is {@code null} it is the equivalent of
299 * specifying the address as the AnyLocal address
300 * (see {@link java.net.InetAddress#isAnyLocalAddress InetAddress.isAnyLocalAddress}{@code ()}).
301 * <p>
302 * A local port number of {@code zero} will let the system pick up a
303 * free port in the {@code bind} operation.</p>
304 * <p>
306 * {@code checkConnect} method is called
307 * with the host address and {@code port}
308 * as its arguments. This could result in a SecurityException.
309 *
310 * @param address the remote address
311 * @param port the remote port
312 * @param localAddr the local address the socket is bound to, or
313 * {@code null} for the {@code anyLocal} address.
314 * @param localPort the local port the socket is bound to or
315 * {@code zero} for a system selected free port.
316 * @exception IOException if an I/O error occurs when creating the socket.
317 * @exception SecurityException if a security manager exists and its
318 * {@code checkConnect} method doesn't allow the connection
319 * to the destination, or if its {@code checkListen} method
320 * doesn't allow the bind to the local port.
321 * @exception IllegalArgumentException if the port parameter or localPort
322 * parameter is outside the specified range of valid port values,
323 * which is between 0 and 65535, inclusive.
324 * @exception NullPointerException if {@code address} is null.
325 * @see SecurityManager#checkConnect
326 * @since 1.1
327 */
328 public Socket(InetAddress address, int port, InetAddress localAddr,
329 int localPort) throws IOException {
330 this(address != null ? new InetSocketAddress(address, port) : null,
331 new InetSocketAddress(localAddr, localPort), true);
332 }
333
334 /**
335 * Creates a stream socket and connects it to the specified port
336 * number on the named host.
337 * <p>
338 * If the specified host is {@code null} it is the equivalent of
339 * specifying the address as
340 * {@link java.net.InetAddress#getByName InetAddress.getByName}{@code (null)}.
341 * In other words, it is equivalent to specifying an address of the
342 * loopback interface. </p>
343 * <p>
344 * If the stream argument is {@code true}, this creates a
345 * stream socket. If the stream argument is {@code false}, it
346 * creates a datagram socket.
691 if (!isConnected())
692 return null;
693 try {
694 return getImpl().getInetAddress();
695 } catch (SocketException e) {
696 }
697 return null;
698 }
699
700 /**
701 * Gets the local address to which the socket is bound.
702 * <p>
703 * If there is a security manager set, its {@code checkConnect} method is
704 * called with the local address and {@code -1} as its arguments to see
705 * if the operation is allowed. If the operation is not allowed,
706 * the {@link InetAddress#getLoopbackAddress loopback} address is returned.
707 *
708 * @return the local address to which the socket is bound,
709 * the loopback address if denied by the security manager, or
710 * the wildcard address if the socket is closed or not bound yet.
711 * @since 1.1
712 *
713 * @see SecurityManager#checkConnect
714 */
715 public InetAddress getLocalAddress() {
716 // This is for backward compatibility
717 if (!isBound())
718 return InetAddress.anyLocalAddress();
719 InetAddress in = null;
720 try {
721 in = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR);
722 SecurityManager sm = System.getSecurityManager();
723 if (sm != null)
724 sm.checkConnect(in.getHostAddress(), -1);
725 if (in.isAnyLocalAddress()) {
726 in = InetAddress.anyLocalAddress();
727 }
728 } catch (SecurityException e) {
729 in = InetAddress.getLoopbackAddress();
730 } catch (Exception e) {
731 in = InetAddress.anyLocalAddress(); // "0.0.0.0"
955 public OutputStream run() throws IOException {
956 return impl.getOutputStream();
957 }
958 });
959 } catch (java.security.PrivilegedActionException e) {
960 throw (IOException) e.getException();
961 }
962 return os;
963 }
964
965 /**
966 * Enable/disable {@link SocketOptions#TCP_NODELAY TCP_NODELAY}
967 * (disable/enable Nagle's algorithm).
968 *
969 * @param on {@code true} to enable TCP_NODELAY,
970 * {@code false} to disable.
971 *
972 * @exception SocketException if there is an error
973 * in the underlying protocol, such as a TCP error.
974 *
975 * @since 1.1
976 *
977 * @see #getTcpNoDelay()
978 */
979 public void setTcpNoDelay(boolean on) throws SocketException {
980 if (isClosed())
981 throw new SocketException("Socket is closed");
982 getImpl().setOption(SocketOptions.TCP_NODELAY, Boolean.valueOf(on));
983 }
984
985 /**
986 * Tests if {@link SocketOptions#TCP_NODELAY TCP_NODELAY} is enabled.
987 *
988 * @return a {@code boolean} indicating whether or not
989 * {@link SocketOptions#TCP_NODELAY TCP_NODELAY} is enabled.
990 * @exception SocketException if there is an error
991 * in the underlying protocol, such as a TCP error.
992 * @since 1.1
993 * @see #setTcpNoDelay(boolean)
994 */
995 public boolean getTcpNoDelay() throws SocketException {
996 if (isClosed())
997 throw new SocketException("Socket is closed");
998 return ((Boolean) getImpl().getOption(SocketOptions.TCP_NODELAY)).booleanValue();
999 }
1000
1001 /**
1002 * Enable/disable {@link SocketOptions#SO_LINGER SO_LINGER} with the
1003 * specified linger time in seconds. The maximum timeout value is platform
1004 * specific.
1005 *
1006 * The setting only affects socket close.
1007 *
1008 * @param on whether or not to linger on.
1009 * @param linger how long to linger for, if on is true.
1010 * @exception SocketException if there is an error
1011 * in the underlying protocol, such as a TCP error.
1012 * @exception IllegalArgumentException if the linger value is negative.
1013 * @since 1.1
1014 * @see #getSoLinger()
1015 */
1016 public void setSoLinger(boolean on, int linger) throws SocketException {
1017 if (isClosed())
1018 throw new SocketException("Socket is closed");
1019 if (!on) {
1020 getImpl().setOption(SocketOptions.SO_LINGER, on);
1021 } else {
1022 if (linger < 0) {
1023 throw new IllegalArgumentException("invalid value for SO_LINGER");
1024 }
1025 if (linger > 65535)
1026 linger = 65535;
1027 getImpl().setOption(SocketOptions.SO_LINGER, new Integer(linger));
1028 }
1029 }
1030
1031 /**
1032 * Returns setting for {@link SocketOptions#SO_LINGER SO_LINGER}.
1033 * -1 returns implies that the
1034 * option is disabled.
1035 *
1036 * The setting only affects socket close.
1037 *
1038 * @return the setting for {@link SocketOptions#SO_LINGER SO_LINGER}.
1039 * @exception SocketException if there is an error
1040 * in the underlying protocol, such as a TCP error.
1041 * @since 1.1
1042 * @see #setSoLinger(boolean, int)
1043 */
1044 public int getSoLinger() throws SocketException {
1045 if (isClosed())
1046 throw new SocketException("Socket is closed");
1047 Object o = getImpl().getOption(SocketOptions.SO_LINGER);
1048 if (o instanceof Integer) {
1049 return ((Integer) o).intValue();
1050 } else {
1051 return -1;
1052 }
1053 }
1054
1055 /**
1056 * Send one byte of urgent data on the socket. The byte to be sent is the lowest eight
1057 * bits of the data parameter. The urgent byte is
1058 * sent after any preceding writes to the socket OutputStream
1059 * and before any future writes to the OutputStream.
1060 * @param data The byte of data to send
1061 * @exception IOException if there is an error
1114 public boolean getOOBInline() throws SocketException {
1115 if (isClosed())
1116 throw new SocketException("Socket is closed");
1117 return ((Boolean) getImpl().getOption(SocketOptions.SO_OOBINLINE)).booleanValue();
1118 }
1119
1120 /**
1121 * Enable/disable {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}
1122 * with the specified timeout, in milliseconds. With this option set
1123 * to a non-zero timeout, a read() call on the InputStream associated with
1124 * this Socket will block for only this amount of time. If the timeout
1125 * expires, a <B>java.net.SocketTimeoutException</B> is raised, though the
1126 * Socket is still valid. The option <B>must</B> be enabled
1127 * prior to entering the blocking operation to have effect. The
1128 * timeout must be {@code > 0}.
1129 * A timeout of zero is interpreted as an infinite timeout.
1130 *
1131 * @param timeout the specified timeout, in milliseconds.
1132 * @exception SocketException if there is an error
1133 * in the underlying protocol, such as a TCP error.
1134 * @since 1.1
1135 * @see #getSoTimeout()
1136 */
1137 public synchronized void setSoTimeout(int timeout) throws SocketException {
1138 if (isClosed())
1139 throw new SocketException("Socket is closed");
1140 if (timeout < 0)
1141 throw new IllegalArgumentException("timeout can't be negative");
1142
1143 getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
1144 }
1145
1146 /**
1147 * Returns setting for {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}.
1148 * 0 returns implies that the option is disabled (i.e., timeout of infinity).
1149 *
1150 * @return the setting for {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}
1151 * @exception SocketException if there is an error
1152 * in the underlying protocol, such as a TCP error.
1153 *
1154 * @since 1.1
1155 * @see #setSoTimeout(int)
1156 */
1157 public synchronized int getSoTimeout() throws SocketException {
1158 if (isClosed())
1159 throw new SocketException("Socket is closed");
1160 Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT);
1161 /* extra type safety */
1162 if (o instanceof Integer) {
1163 return ((Integer) o).intValue();
1164 } else {
1165 return 0;
1166 }
1167 }
1168
1169 /**
1170 * Sets the {@link SocketOptions#SO_SNDBUF SO_SNDBUF} option to the
1171 * specified value for this {@code Socket}.
1172 * The {@link SocketOptions#SO_SNDBUF SO_SNDBUF} option is used by the
1173 * platform's networking code as a hint for the size to set the underlying
1174 * network I/O buffers.
|