1. /*
  2. * @(#)SocketImpl.java 1.28 01/11/29
  3. *
  4. * Copyright 2002 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.28, 11/29/01
  22. * @since JDK1.0
  23. */
  24. public abstract class SocketImpl implements SocketOptions {
  25. /**
  26. * The file descriptor object for this socket.
  27. */
  28. protected FileDescriptor fd;
  29. /**
  30. * The IP address of the remote end of this socket.
  31. */
  32. protected InetAddress address;
  33. /**
  34. * The port number on the remote host to which this socket is connected.
  35. */
  36. protected int port;
  37. /**
  38. * The local port number to which this socket is connected.
  39. */
  40. protected int localport;
  41. /**
  42. * Creates either a stream or a datagram socket.
  43. *
  44. * @param stream if <code>true</code>, create a stream socket;
  45. * otherwise, create a datagram socket.
  46. * @exception IOException if an I/O error occurs while creating the
  47. * socket.
  48. */
  49. protected abstract void create(boolean stream) throws IOException;
  50. /**
  51. * Connects this socket to the specified port on the named host.
  52. *
  53. * @param host the name of the remote host.
  54. * @param port the port number.
  55. * @exception IOException if an I/O error occurs when connecting to the
  56. * remote host.
  57. */
  58. protected abstract void connect(String host, int port) throws IOException;
  59. /**
  60. * Connects this socket to the specified port number on the specified host.
  61. *
  62. * @param address the IP address of the remote host.
  63. * @param port the port number.
  64. * @exception IOException if an I/O error occurs when attempting a
  65. * connection.
  66. */
  67. protected abstract void connect(InetAddress address, int port) throws IOException;
  68. /**
  69. * Binds this socket to the specified port number on the specified host.
  70. *
  71. * @param host the IP address of the remote host.
  72. * @param port the port number.
  73. * @exception IOException if an I/O error occurs when binding this socket.
  74. */
  75. protected abstract void bind(InetAddress host, int port) throws IOException;
  76. /**
  77. * Sets the maximum queue length for incoming connection indications
  78. * (a request to connect) to the <code>count</code> argument. If a
  79. * connection indication arrives when the queue is full, the
  80. * connection is refused.
  81. *
  82. * @param backlog the maximum length of the queue.
  83. * @exception IOException if an I/O error occurs when creating the queue.
  84. */
  85. protected abstract void listen(int backlog) throws IOException;
  86. /**
  87. * Accepts a connection.
  88. *
  89. * @param s the accepted connection.
  90. * @exception IOException if an I/O error occurs when accepting the
  91. * connection.
  92. */
  93. protected abstract void accept(SocketImpl s) throws IOException;
  94. /**
  95. * Returns an input stream for this socket.
  96. *
  97. * @return a stream for reading from this socket.
  98. * @exception IOException if an I/O error occurs when creating the
  99. * input stream.
  100. */
  101. protected abstract InputStream getInputStream() throws IOException;
  102. /**
  103. * Returns an output stream for this socket.
  104. *
  105. * @return an output stream for writing to this socket.
  106. * @exception IOException if an I/O error occurs when creating the
  107. * output stream.
  108. */
  109. protected abstract OutputStream getOutputStream() throws IOException;
  110. /**
  111. * Returns the number of bytes that can be read from this socket
  112. * without blocking.
  113. *
  114. * @return the number of bytes that can be read from this socket
  115. * without blocking.
  116. * @exception IOException if an I/O error occurs when determining the
  117. * number of bytes available.
  118. */
  119. protected abstract int available() throws IOException;
  120. /**
  121. * Closes this socket.
  122. *
  123. * @exception IOException if an I/O error occurs when closing this socket.
  124. */
  125. protected abstract void close() throws IOException;
  126. /**
  127. * Returns the value of this socket's <code>fd</code> field.
  128. *
  129. * @return the value of this socket's <code>fd</code> field.
  130. * @see java.net.SocketImpl#fd
  131. */
  132. protected FileDescriptor getFileDescriptor() {
  133. return fd;
  134. }
  135. /**
  136. * Returns the value of this socket's <code>address</code> field.
  137. *
  138. * @return the value of this socket's <code>address</code> field.
  139. * @see java.net.SocketImpl#address
  140. */
  141. protected InetAddress getInetAddress() {
  142. return address;
  143. }
  144. /**
  145. * Returns the value of this socket's <code>port</code> field.
  146. *
  147. * @return the value of this socket's <code>port</code> field.
  148. * @see java.net.SocketImpl#port
  149. */
  150. protected int getPort() {
  151. return port;
  152. }
  153. /**
  154. * Returns the value of this socket's <code>localport</code> field.
  155. *
  156. * @return the value of this socket's <code>localport</code> field.
  157. * @see java.net.SocketImpl#localport
  158. */
  159. protected int getLocalPort() {
  160. return localport;
  161. }
  162. /**
  163. * Returns the address and port of this socket as a <code>String</code>.
  164. *
  165. * @return a string representation of this socket.
  166. */
  167. public String toString() {
  168. return "Socket[addr=" + getInetAddress() +
  169. ",port=" + getPort() + ",localport=" + getLocalPort() + "]";
  170. }
  171. void reset() throws IOException {
  172. address = null;
  173. port = 0;
  174. localport = 0;
  175. close();
  176. }
  177. }