1. /*
  2. * @(#)SocketImpl.java 1.42 04/03/25
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.net;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.OutputStream;
  11. import java.io.FileDescriptor;
  12. /**
  13. * The abstract class <code>SocketImpl</code> is a common superclass
  14. * of all classes that actually implement sockets. It is used to
  15. * create both client and server sockets.
  16. * <p>
  17. * A "plain" socket implements these methods exactly as
  18. * described, without attempting to go through a firewall or proxy.
  19. *
  20. * @author unascribed
  21. * @version 1.42, 03/25/04
  22. * @since JDK1.0
  23. */
  24. public abstract class SocketImpl implements SocketOptions {
  25. /**
  26. * The actual Socket object.
  27. */
  28. Socket socket = null;
  29. ServerSocket serverSocket = null;
  30. /**
  31. * The file descriptor object for this socket.
  32. */
  33. protected FileDescriptor fd;
  34. /**
  35. * The IP address of the remote end of this socket.
  36. */
  37. protected InetAddress address;
  38. /**
  39. * The port number on the remote host to which this socket is connected.
  40. */
  41. protected int port;
  42. /**
  43. * The local port number to which this socket is connected.
  44. */
  45. protected int localport;
  46. /**
  47. * Creates either a stream or a datagram socket.
  48. *
  49. * @param stream if <code>true</code>, create a stream socket;
  50. * otherwise, create a datagram socket.
  51. * @exception IOException if an I/O error occurs while creating the
  52. * socket.
  53. */
  54. protected abstract void create(boolean stream) throws IOException;
  55. /**
  56. * Connects this socket to the specified port on the named host.
  57. *
  58. * @param host the name of the remote host.
  59. * @param port the port number.
  60. * @exception IOException if an I/O error occurs when connecting to the
  61. * remote host.
  62. */
  63. protected abstract void connect(String host, int port) throws IOException;
  64. /**
  65. * Connects this socket to the specified port number on the specified host.
  66. *
  67. * @param address the IP address of the remote host.
  68. * @param port the port number.
  69. * @exception IOException if an I/O error occurs when attempting a
  70. * connection.
  71. */
  72. protected abstract void connect(InetAddress address, int port) throws IOException;
  73. /**
  74. * Connects this socket to the specified port number on the specified host.
  75. * A timeout of zero is interpreted as an infinite timeout. The connection
  76. * will then block until established or an error occurs.
  77. *
  78. * @param address the Socket address of the remote host.
  79. * @param timeout the timeout value, in milliseconds, or zero for no timeout.
  80. * @exception IOException if an I/O error occurs when attempting a
  81. * connection.
  82. * @since 1.4
  83. */
  84. protected abstract void connect(SocketAddress address, int timeout) throws IOException;
  85. /**
  86. * Binds this socket to the specified local IP address and port number.
  87. *
  88. * @param host an IP address that belongs to a local interface.
  89. * @param port the port number.
  90. * @exception IOException if an I/O error occurs when binding this socket.
  91. */
  92. protected abstract void bind(InetAddress host, int port) throws IOException;
  93. /**
  94. * Sets the maximum queue length for incoming connection indications
  95. * (a request to connect) to the <code>count</code> argument. If a
  96. * connection indication arrives when the queue is full, the
  97. * connection is refused.
  98. *
  99. * @param backlog the maximum length of the queue.
  100. * @exception IOException if an I/O error occurs when creating the queue.
  101. */
  102. protected abstract void listen(int backlog) throws IOException;
  103. /**
  104. * Accepts a connection.
  105. *
  106. * @param s the accepted connection.
  107. * @exception IOException if an I/O error occurs when accepting the
  108. * connection.
  109. */
  110. protected abstract void accept(SocketImpl s) throws IOException;
  111. /**
  112. * Returns an input stream for this socket.
  113. *
  114. * @return a stream for reading from this socket.
  115. * @exception IOException if an I/O error occurs when creating the
  116. * input stream.
  117. */
  118. protected abstract InputStream getInputStream() throws IOException;
  119. /**
  120. * Returns an output stream for this socket.
  121. *
  122. * @return an output stream for writing to this socket.
  123. * @exception IOException if an I/O error occurs when creating the
  124. * output stream.
  125. */
  126. protected abstract OutputStream getOutputStream() throws IOException;
  127. /**
  128. * Returns the number of bytes that can be read from this socket
  129. * without blocking.
  130. *
  131. * @return the number of bytes that can be read from this socket
  132. * without blocking.
  133. * @exception IOException if an I/O error occurs when determining the
  134. * number of bytes available.
  135. */
  136. protected abstract int available() throws IOException;
  137. /**
  138. * Closes this socket.
  139. *
  140. * @exception IOException if an I/O error occurs when closing this socket.
  141. */
  142. protected abstract void close() throws IOException;
  143. /**
  144. * Places the input stream for this socket at "end of stream".
  145. * Any data sent to this socket is acknowledged and then
  146. * silently discarded.
  147. *
  148. * If you read from a socket input stream after invoking
  149. * shutdownInput() on the socket, the stream will return EOF.
  150. *
  151. * @exception IOException if an I/O error occurs when shutting down this
  152. * socket.
  153. * @see java.net.Socket#shutdownOutput()
  154. * @see java.net.Socket#close()
  155. * @see java.net.Socket#setSoLinger(boolean, int)
  156. */
  157. protected void shutdownInput() throws IOException {
  158. throw new IOException("Method not implemented!");
  159. }
  160. /**
  161. * Disables the output stream for this socket.
  162. * For a TCP socket, any previously written data will be sent
  163. * followed by TCP's normal connection termination sequence.
  164. *
  165. * If you write to a socket output stream after invoking
  166. * shutdownOutput() on the socket, the stream will throw
  167. * an IOException.
  168. *
  169. * @exception IOException if an I/O error occurs when shutting down this
  170. * socket.
  171. * @see java.net.Socket#shutdownInput()
  172. * @see java.net.Socket#close()
  173. * @see java.net.Socket#setSoLinger(boolean, int)
  174. */
  175. protected void shutdownOutput() throws IOException {
  176. throw new IOException("Method not implemented!");
  177. }
  178. /**
  179. * Returns the value of this socket's <code>fd</code> field.
  180. *
  181. * @return the value of this socket's <code>fd</code> field.
  182. * @see java.net.SocketImpl#fd
  183. */
  184. protected FileDescriptor getFileDescriptor() {
  185. return fd;
  186. }
  187. /**
  188. * Returns the value of this socket's <code>address</code> field.
  189. *
  190. * @return the value of this socket's <code>address</code> field.
  191. * @see java.net.SocketImpl#address
  192. */
  193. protected InetAddress getInetAddress() {
  194. return address;
  195. }
  196. /**
  197. * Returns the value of this socket's <code>port</code> field.
  198. *
  199. * @return the value of this socket's <code>port</code> field.
  200. * @see java.net.SocketImpl#port
  201. */
  202. protected int getPort() {
  203. return port;
  204. }
  205. /**
  206. * Returns whether or not this SocketImpl supports sending
  207. * urgent data. By default, false is returned
  208. * unless the method is overridden in a sub-class
  209. *
  210. * @return true if urgent data supported
  211. * @see java.net.SocketImpl#address
  212. * @since 1.4
  213. */
  214. protected boolean supportsUrgentData () {
  215. return false; // must be overridden in sub-class
  216. }
  217. /**
  218. * Send one byte of urgent data on the socket.
  219. * The byte to be sent is the low eight bits of the parameter
  220. * @param data The byte of data to send
  221. * @exception IOException if there is an error
  222. * sending the data.
  223. * @since 1.4
  224. */
  225. protected abstract void sendUrgentData (int data) throws IOException;
  226. /**
  227. * Returns the value of this socket's <code>localport</code> field.
  228. *
  229. * @return the value of this socket's <code>localport</code> field.
  230. * @see java.net.SocketImpl#localport
  231. */
  232. protected int getLocalPort() {
  233. return localport;
  234. }
  235. void setSocket(Socket soc) {
  236. this.socket = soc;
  237. }
  238. Socket getSocket() {
  239. return socket;
  240. }
  241. void setServerSocket(ServerSocket soc) {
  242. this.serverSocket = soc;
  243. }
  244. ServerSocket getServerSocket() {
  245. return serverSocket;
  246. }
  247. /**
  248. * Returns the address and port of this socket as a <code>String</code>.
  249. *
  250. * @return a string representation of this socket.
  251. */
  252. public String toString() {
  253. return "Socket[addr=" + getInetAddress() +
  254. ",port=" + getPort() + ",localport=" + getLocalPort() + "]";
  255. }
  256. void reset() throws IOException {
  257. address = null;
  258. port = 0;
  259. localport = 0;
  260. close();
  261. }
  262. /**
  263. * Sets performance preferences for this socket.
  264. *
  265. * <p> Sockets use the TCP/IP protocol by default. Some implementations
  266. * may offer alternative protocols which have different performance
  267. * characteristics than TCP/IP. This method allows the application to
  268. * express its own preferences as to how these tradeoffs should be made
  269. * when the implementation chooses from the available protocols.
  270. *
  271. * <p> Performance preferences are described by three integers
  272. * whose values indicate the relative importance of short connection time,
  273. * low latency, and high bandwidth. The absolute values of the integers
  274. * are irrelevant; in order to choose a protocol the values are simply
  275. * compared, with larger values indicating stronger preferences. Negative
  276. * values represent a lower priority than positive values. If the
  277. * application prefers short connection time over both low latency and high
  278. * bandwidth, for example, then it could invoke this method with the values
  279. * <tt>(1, 0, 0)</tt>. If the application prefers high bandwidth above low
  280. * latency, and low latency above short connection time, then it could
  281. * invoke this method with the values <tt>(0, 1, 2)</tt>.
  282. *
  283. * By default, this method does nothing, unless it is overridden in a
  284. * a sub-class.
  285. *
  286. * @param connectionTime
  287. * An <tt>int</tt> expressing the relative importance of a short
  288. * connection time
  289. *
  290. * @param latency
  291. * An <tt>int</tt> expressing the relative importance of low
  292. * latency
  293. *
  294. * @param bandwidth
  295. * An <tt>int</tt> expressing the relative importance of high
  296. * bandwidth
  297. *
  298. * @since 1.5
  299. */
  300. protected void setPerformancePreferences(int connectionTime,
  301. int latency,
  302. int bandwidth)
  303. {
  304. /* Not implemented yet */
  305. }
  306. }