1. /*
  2. * @(#)SocketImpl.java 1.30 00/08/16
  3. *
  4. * Copyright 1995-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. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.OutputStream;
  14. import java.io.FileDescriptor;
  15. /**
  16. * The abstract class <code>SocketImpl</code> is a common superclass
  17. * of all classes that actually implement sockets. It is used to
  18. * create both client and server sockets.
  19. * <p>
  20. * A "plain" socket implements these methods exactly as
  21. * described, without attempting to go through a firewall or proxy.
  22. *
  23. * @author unascribed
  24. * @version 1.30, 08/16/00
  25. * @since JDK1.0
  26. */
  27. public abstract class SocketImpl implements SocketOptions {
  28. /**
  29. * The file descriptor object for this socket.
  30. */
  31. protected FileDescriptor fd;
  32. /**
  33. * The IP address of the remote end of this socket.
  34. */
  35. protected InetAddress address;
  36. /**
  37. * The port number on the remote host to which this socket is connected.
  38. */
  39. protected int port;
  40. /**
  41. * The local port number to which this socket is connected.
  42. */
  43. protected int localport;
  44. /**
  45. * Creates either a stream or a datagram socket.
  46. *
  47. * @param stream if <code>true</code>, create a stream socket;
  48. * otherwise, create a datagram socket.
  49. * @exception IOException if an I/O error occurs while creating the
  50. * socket.
  51. */
  52. protected abstract void create(boolean stream) throws IOException;
  53. /**
  54. * Connects this socket to the specified port on the named host.
  55. *
  56. * @param host the name of the remote host.
  57. * @param port the port number.
  58. * @exception IOException if an I/O error occurs when connecting to the
  59. * remote host.
  60. */
  61. protected abstract void connect(String host, int port) throws IOException;
  62. /**
  63. * Connects this socket to the specified port number on the specified host.
  64. *
  65. * @param address the IP address of the remote host.
  66. * @param port the port number.
  67. * @exception IOException if an I/O error occurs when attempting a
  68. * connection.
  69. */
  70. protected abstract void connect(InetAddress address, int port) throws IOException;
  71. /**
  72. * Binds this socket to the specified port number on the specified host.
  73. *
  74. * @param host the IP address of the remote host.
  75. * @param port the port number.
  76. * @exception IOException if an I/O error occurs when binding this socket.
  77. */
  78. protected abstract void bind(InetAddress host, int port) throws IOException;
  79. /**
  80. * Sets the maximum queue length for incoming connection indications
  81. * (a request to connect) to the <code>count</code> argument. If a
  82. * connection indication arrives when the queue is full, the
  83. * connection is refused.
  84. *
  85. * @param backlog the maximum length of the queue.
  86. * @exception IOException if an I/O error occurs when creating the queue.
  87. */
  88. protected abstract void listen(int backlog) throws IOException;
  89. /**
  90. * Accepts a connection.
  91. *
  92. * @param s the accepted connection.
  93. * @exception IOException if an I/O error occurs when accepting the
  94. * connection.
  95. */
  96. protected abstract void accept(SocketImpl s) throws IOException;
  97. /**
  98. * Returns an input stream for this socket.
  99. *
  100. * @return a stream for reading from this socket.
  101. * @exception IOException if an I/O error occurs when creating the
  102. * input stream.
  103. */
  104. protected abstract InputStream getInputStream() throws IOException;
  105. /**
  106. * Returns an output stream for this socket.
  107. *
  108. * @return an output stream for writing to this socket.
  109. * @exception IOException if an I/O error occurs when creating the
  110. * output stream.
  111. */
  112. protected abstract OutputStream getOutputStream() throws IOException;
  113. /**
  114. * Returns the number of bytes that can be read from this socket
  115. * without blocking.
  116. *
  117. * @return the number of bytes that can be read from this socket
  118. * without blocking.
  119. * @exception IOException if an I/O error occurs when determining the
  120. * number of bytes available.
  121. */
  122. protected abstract int available() throws IOException;
  123. /**
  124. * Closes this socket.
  125. *
  126. * @exception IOException if an I/O error occurs when closing this socket.
  127. */
  128. protected abstract void close() throws IOException;
  129. /**
  130. * Places the input stream for this socket at "end of stream".
  131. * Any data sent to this socket is acknowledged and then
  132. * silently discarded.
  133. *
  134. * If you read from a socket input stream after invoking
  135. * shutdownInput() on the socket, the stream will return EOF.
  136. *
  137. * @exception IOException if an I/O error occurs when shutting down this
  138. * socket.
  139. * @see java.net.Socket#shutdownOutput()
  140. * @see java.net.Socket#close()
  141. * @see java.net.Socket#setSoLinger(boolean, int)
  142. */
  143. protected void shutdownInput() throws IOException {
  144. throw new IOException("Method not implemented!");
  145. }
  146. /**
  147. * Disables the output stream for this socket.
  148. * For a TCP socket, any previously written data will be sent
  149. * followed by TCP's normal connection termination sequence.
  150. *
  151. * If you write to a socket output stream after invoking
  152. * shutdownOutput() on the socket, the stream will throw
  153. * an IOException.
  154. *
  155. * @exception IOException if an I/O error occurs when shutting down this
  156. * socket.
  157. * @see java.net.Socket#shutdownInput()
  158. * @see java.net.Socket#close()
  159. * @see java.net.Socket#setSoLinger(boolean, int)
  160. */
  161. protected void shutdownOutput() throws IOException {
  162. throw new IOException("Method not implemented!");
  163. }
  164. /**
  165. * Returns the value of this socket's <code>fd</code> field.
  166. *
  167. * @return the value of this socket's <code>fd</code> field.
  168. * @see java.net.SocketImpl#fd
  169. */
  170. protected FileDescriptor getFileDescriptor() {
  171. return fd;
  172. }
  173. /**
  174. * Returns the value of this socket's <code>address</code> field.
  175. *
  176. * @return the value of this socket's <code>address</code> field.
  177. * @see java.net.SocketImpl#address
  178. */
  179. protected InetAddress getInetAddress() {
  180. return address;
  181. }
  182. /**
  183. * Returns the value of this socket's <code>port</code> field.
  184. *
  185. * @return the value of this socket's <code>port</code> field.
  186. * @see java.net.SocketImpl#port
  187. */
  188. protected int getPort() {
  189. return port;
  190. }
  191. /**
  192. * Returns the value of this socket's <code>localport</code> field.
  193. *
  194. * @return the value of this socket's <code>localport</code> field.
  195. * @see java.net.SocketImpl#localport
  196. */
  197. protected int getLocalPort() {
  198. return localport;
  199. }
  200. /**
  201. * Returns the address and port of this socket as a <code>String</code>.
  202. *
  203. * @return a string representation of this socket.
  204. */
  205. public String toString() {
  206. return "Socket[addr=" + getInetAddress() +
  207. ",port=" + getPort() + ",localport=" + getLocalPort() + "]";
  208. }
  209. void reset() throws IOException {
  210. address = null;
  211. port = 0;
  212. localport = 0;
  213. close();
  214. }
  215. }