1. /*
  2. * @(#)SocketOptions.java 1.20 00/02/02
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.net;
  11. /**
  12. * Interface of methods to get/set socket options. This interface is
  13. * implemented by: <B>SocketImpl</B> and <B>DatagramSocketImpl</B>.
  14. * Subclasses of these should override the methods
  15. * of this interface in order to support their own options.
  16. * <P>
  17. * The methods and constants which specify options in this interface are
  18. * for implementation only. If you're not subclassing SocketImpl or
  19. * DatagramSocketImpl, <B>you won't use these directly.</B> There are
  20. * type-safe methods to get/set each of these options in Socket, ServerSocket,
  21. * DatagramSocket and MulticastSocket.
  22. * <P>
  23. * A subset of the standard BSD-style socket options are supported in the
  24. * base classes, <B>PlainSocketImpl</B> and <B>PlainDatagramSocketImpl</B>.
  25. * A brief description of each and their use is provided.
  26. * <P>
  27. * @version 1.20, 02/02/00
  28. * @author David Brown
  29. */
  30. public interface SocketOptions {
  31. /**
  32. * Enable/disable the option specified by <I>optID</I>. If the option
  33. * is to be enabled, and it takes an option-specific "value", this is
  34. * passed in <I>value</I>. The actual type of value is option-specific,
  35. * and it is an error to pass something that isn't of the expected type:
  36. * <BR><PRE>
  37. * SocketImpl s;
  38. * ...
  39. * s.setOption(SO_LINGER, new Integer(10));
  40. * // OK - set SO_LINGER w/ timeout of 10 sec.
  41. * s.setOption(SO_LINGER, new Double(10));
  42. * // ERROR - expects java.lang.Integer
  43. *</PRE>
  44. * If the requested option is binary, it can be set using this method by
  45. * a java.lang.Boolean:
  46. * <BR><PRE>
  47. * s.setOption(TCP_NODELAY, new Boolean(true));
  48. * // OK - enables TCP_NODELAY, a binary option
  49. * </PRE>
  50. * <BR>
  51. * Any option can be disabled using this method with a Boolean(false):
  52. * <BR><PRE>
  53. * s.setOption(TCP_NODELAY, new Boolean(false));
  54. * // OK - disables TCP_NODELAY
  55. * s.setOption(SO_LINGER, new Boolean(false));
  56. * // OK - disables SO_LINGER
  57. * </PRE>
  58. * <BR>
  59. * For an option that has a notion of on and off, and requires
  60. * a non-boolean parameter, setting its value to anything other than
  61. * <I>Boolean(false)</I> implicitly enables it.
  62. * <BR>
  63. * Throws SocketException if the option is unrecognized,
  64. * the socket is closed, or some low-level error occurred
  65. * <BR>
  66. * @param optID identifies the option
  67. * @param value the parameter of the socket option
  68. * @throws SocketException if the option is unrecognized,
  69. * the socket is closed, or some low-level error occurred
  70. * @see #getOption(int)
  71. */
  72. public void
  73. setOption(int optID, Object value) throws SocketException;
  74. /**
  75. * Fetch the value of an option.
  76. * Binary options will return java.lang.Boolean(true)
  77. * if enabled, java.lang.Boolean(false) if disabled, e.g.:
  78. * <BR><PRE>
  79. * SocketImpl s;
  80. * ...
  81. * Boolean noDelay = (Boolean)(s.getOption(TCP_NODELAY));
  82. * if (noDelay.booleanValue()) {
  83. * // true if TCP_NODELAY is enabled...
  84. * ...
  85. * }
  86. * </PRE>
  87. * <P>
  88. * For options that take a particular type as a parameter,
  89. * getOption(int) will return the paramter's value, else
  90. * it will return java.lang.Boolean(false):
  91. * <PRE>
  92. * Object o = s.getOption(SO_LINGER);
  93. * if (o instanceof Integer) {
  94. * System.out.print("Linger time is " + ((Integer)o).intValue());
  95. * } else {
  96. * // the true type of o is java.lang.Boolean(false);
  97. * }
  98. * </PRE>
  99. *
  100. * @param optID an <code>int</code> identifying the option to fetch
  101. * @return the value of the option
  102. * @throws SocketException if the socket is closed
  103. * @throws SocketException if <I>optID</I> is unknown along the
  104. * protocol stack (including the SocketImpl)
  105. * @see #setOption(int, java.lang.Object)
  106. */
  107. public Object getOption(int optID) throws SocketException;
  108. /**
  109. * The java-supported BSD-style options.
  110. */
  111. /**
  112. * Disable Nagle's algorithm for this connection. Written data
  113. * to the network is not buffered pending acknowledgement of
  114. * previously written data.
  115. *<P>
  116. * Valid for TCP only: SocketImpl.
  117. * <P>
  118. * @see Socket#setTcpNoDelay
  119. * @see Socket#getTcpNoDelay
  120. */
  121. public final static int TCP_NODELAY = 0x0001;
  122. /**
  123. * Fetch the local address binding of a socket (this option cannot
  124. * be "set" only "gotten", since sockets are bound at creation time,
  125. * and so the locally bound address cannot be changed). The default local
  126. * address of a socket is INADDR_ANY, meaning any local address on a
  127. * multi-homed host. A multi-homed host can use this option to accept
  128. * connections to only one of its addresses (in the case of a
  129. * ServerSocket or DatagramSocket), or to specify its return address
  130. * to the peer (for a Socket or DatagramSocket). The parameter of
  131. * this option is an InetAddress.
  132. * <P>
  133. * This option <B>must</B> be specified in the constructor.
  134. * <P>
  135. * Valid for: SocketImpl, DatagramSocketImpl
  136. * <P>
  137. * @see Socket#getLocalAddress
  138. * @see DatagramSocket#getLocalAddress
  139. */
  140. public final static int SO_BINDADDR = 0x000F;
  141. /** Sets SO_REUSEADDR for a socket. This is used only for MulticastSockets
  142. * in java, and it is set by default for MulticastSockets.
  143. * <P>
  144. * Valid for: DatagramSocketImpl
  145. */
  146. public final static int SO_REUSEADDR = 0x04;
  147. /** Set which outgoing interface on which to send multicast packets.
  148. * Useful on hosts with multiple network interfaces, where applications
  149. * want to use other than the system default. Takes/returns an InetAddress.
  150. * <P>
  151. * Valid for Multicast: DatagramSocketImpl
  152. * <P>
  153. * @see MulticastSocket#setInterface
  154. * @see MulitcastSocket#getInterface
  155. */
  156. public final static int IP_MULTICAST_IF = 0x10;
  157. /**
  158. * Specify a linger-on-close timeout. This option disables/enables
  159. * immediate return from a <B>close()</B> of a TCP Socket. Enabling
  160. * this option with a non-zero Integer <I>timeout</I> means that a
  161. * <B>close()</B> will block pending the transmission and acknowledgement
  162. * of all data written to the peer, at which point the socket is closed
  163. * <I>gracefully</I>. Upon reaching the linger timeout, the socket is
  164. * closed <I>forcefully</I>, with a TCP RST. Enabling the option with a
  165. * timeout of zero does a forceful close immediately. If the specified
  166. * timeout value exceeds 65,535 it will be reduced to 65,535.
  167. * <P>
  168. * Valid only for TCP: SocketImpl
  169. *
  170. * @see Socket#setSoLinger
  171. * @see Socket#getSoLinger
  172. */
  173. public final static int SO_LINGER = 0x0080;
  174. /** Set a timeout on blocking Socket operations:
  175. * <PRE>
  176. * ServerSocket.accept();
  177. * SocketInputStream.read();
  178. * DatagramSocket.receive();
  179. * </PRE>
  180. *
  181. * <P> The option must be set prior to entering a blocking
  182. * operation to take effect. If the timeout expires and the
  183. * operation would continue to block,
  184. * <B>java.io.InterruptedIOException</B> is raised. The Socket is
  185. * not closed in this case.
  186. *
  187. * <P> Valid for all sockets: SocketImpl, DatagramSocketImpl
  188. *
  189. * @see Socket#setSoTimeout
  190. * @see ServerSocket#setSoTimeout
  191. * @see DatagramSocket#setSoTimeout
  192. */
  193. public final static int SO_TIMEOUT = 0x1006;
  194. /**
  195. * Set a hint the size of the underlying buffers used by the
  196. * platform for outgoing network I/O. When used in set, this is a
  197. * suggestion to the kernel from the application about the size of
  198. * buffers to use for the data to be sent over the socket. When
  199. * used in get, this must return the size of the buffer actually
  200. * used by the platform when sending out data on this socket.
  201. *
  202. * Valid for all sockets: SocketImpl, DatagramSocketImpl
  203. *
  204. * @see Socket#setSendBufferSize
  205. * @see Socket#getSendBufferSize
  206. * @see DatagramSocket#setSendBufferSize
  207. * @see DatagramSocket#getSendBufferSize
  208. */
  209. public final static int SO_SNDBUF = 0x1001;
  210. /**
  211. * Set a hint the size of the underlying buffers used by the
  212. * platform for incoming network I/O. When used in set, this is a
  213. * suggestion to the kernel from the application about the size of
  214. * buffers to use for the data to be received over the
  215. * socket. When used in get, this must return the size of the
  216. * buffer actually used by the platform when receiving in data on
  217. * this socket.
  218. *
  219. * Valid for all sockets: SocketImpl, DatagramSocketImpl
  220. *
  221. * @see Socket#setReceiveBufferSize
  222. * @see Socket#getReceiveBufferSize
  223. * @see DatagramSocket#setReceiveBufferSize
  224. * @see DatagramSocket#getReceiveBufferSize
  225. */
  226. public final static int SO_RCVBUF = 0x1002;
  227. /**
  228. * When the keepalive option is set for a TCP socket and no data
  229. * has been exchanged across the socket in either direction for
  230. * 2 hours (NOTE: the actual value is implementation dependent),
  231. * TCP automatically sends a keepalive probe to the peer. This probe is a
  232. * TCP segment to which the peer must respond.
  233. * One of three responses is expected:
  234. * 1. The peer responds with the expected ACK. The application is not
  235. * notified (since everything is OK). TCP will send another probe
  236. * following another 2 hours of inactivity.
  237. * 2. The peer responds with an RST, which tells the local TCP that
  238. * the peer host has crashed and rebooted. The socket is closed.
  239. * 3. There is no response from the peer. The socket is closed.
  240. *
  241. * The purpose of this option is to detect if the peer host crashes.
  242. *
  243. * Valid only for TCP socket: SocketImpl
  244. *
  245. * @see Socket#setKeepAlive
  246. * @see Socket#getKeepAlive
  247. */
  248. public final static int SO_KEEPALIVE = 0x0008;
  249. }