1. /*
  2. * @(#)DatagramSocketImpl.java 1.32 04/05/18
  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.FileDescriptor;
  9. import java.io.IOException;
  10. import java.io.InterruptedIOException;
  11. /**
  12. * Abstract datagram and multicast socket implementation base class.
  13. * @author Pavani Diwanji
  14. * @since JDK1.1
  15. */
  16. public abstract class DatagramSocketImpl implements SocketOptions {
  17. /**
  18. * The local port number.
  19. */
  20. protected int localPort;
  21. /**
  22. * The file descriptor object.
  23. */
  24. protected FileDescriptor fd;
  25. /**
  26. * Creates a datagram socket.
  27. * @exception SocketException if there is an error in the
  28. * underlying protocol, such as a TCP error.
  29. */
  30. protected abstract void create() throws SocketException;
  31. /**
  32. * Binds a datagram socket to a local port and address.
  33. * @param lport the local port
  34. * @param laddr the local address
  35. * @exception SocketException if there is an error in the
  36. * underlying protocol, such as a TCP error.
  37. */
  38. protected abstract void bind(int lport, InetAddress laddr) throws SocketException;
  39. /**
  40. * Sends a datagram packet. The packet contains the data and the
  41. * destination address to send the packet to.
  42. * @param p the packet to be sent.
  43. * @exception IOException if an I/O exception occurs while sending the
  44. * datagram packet.
  45. * @exception PortUnreachableException may be thrown if the socket is connected
  46. * to a currently unreachable destination. Note, there is no guarantee that
  47. * the exception will be thrown.
  48. */
  49. protected abstract void send(DatagramPacket p) throws IOException;
  50. /**
  51. * Connects a datagram socket to a remote destination. This associates the remote
  52. * address with the local socket so that datagrams may only be sent to this destination
  53. * and received from this destination. This may be overridden to call a native
  54. * system connect.
  55. *
  56. * <p>If the remote destination to which the socket is connected does not
  57. * exist, or is otherwise unreachable, and if an ICMP destination unreachable
  58. * packet has been received for that address, then a subsequent call to
  59. * send or receive may throw a PortUnreachableException.
  60. * Note, there is no guarantee that the exception will be thrown.
  61. * @param address the remote InetAddress to connect to
  62. * @param port the remote port number
  63. * @exception SocketException may be thrown if the socket cannot be
  64. * connected to the remote destination
  65. * @since 1.4
  66. */
  67. protected void connect(InetAddress address, int port) throws SocketException {}
  68. /**
  69. * Disconnects a datagram socket from its remote destination.
  70. * @since 1.4
  71. */
  72. protected void disconnect() {}
  73. /**
  74. * Peek at the packet to see who it is from.
  75. * @param i an InetAddress object
  76. * @return the address which the packet came from.
  77. * @exception IOException if an I/O exception occurs
  78. * @exception PortUnreachableException may be thrown if the socket is connected
  79. * to a currently unreachable destination. Note, there is no guarantee that the
  80. * exception will be thrown.
  81. */
  82. protected abstract int peek(InetAddress i) throws IOException;
  83. /**
  84. * Peek at the packet to see who it is from. The data is returned,
  85. * but not consumed, so that a subsequent peekData/receive operation
  86. * will see the same data.
  87. * @param p the Packet Received.
  88. * @return the address which the packet came from.
  89. * @exception IOException if an I/O exception occurs
  90. * @exception PortUnreachableException may be thrown if the socket is connected
  91. * to a currently unreachable destination. Note, there is no guarantee that the
  92. * exception will be thrown.
  93. * @since 1.4
  94. */
  95. protected abstract int peekData(DatagramPacket p) throws IOException;
  96. /**
  97. * Receive the datagram packet.
  98. * @param p the Packet Received.
  99. * @exception IOException if an I/O exception occurs
  100. * while receiving the datagram packet.
  101. * @exception PortUnreachableException may be thrown if the socket is connected
  102. * to a currently unreachable destination. Note, there is no guarantee that the
  103. * exception will be thrown.
  104. */
  105. protected abstract void receive(DatagramPacket p) throws IOException;
  106. /**
  107. * Set the TTL (time-to-live) option.
  108. * @param ttl a byte specifying the TTL value
  109. *
  110. * @deprecated use setTimeToLive instead.
  111. * @exception IOException if an I/O exception occurs while setting
  112. * the time-to-live option.
  113. * @see #getTTL()
  114. */
  115. @Deprecated
  116. protected abstract void setTTL(byte ttl) throws IOException;
  117. /**
  118. * Retrieve the TTL (time-to-live) option.
  119. *
  120. * @exception IOException if an I/O exception occurs
  121. * while retrieving the time-to-live option
  122. * @deprecated use getTimeToLive instead.
  123. * @return a byte representing the TTL value
  124. * @see #setTTL(byte)
  125. */
  126. @Deprecated
  127. protected abstract byte getTTL() throws IOException;
  128. /**
  129. * Set the TTL (time-to-live) option.
  130. * @param ttl an <tt>int</tt> specifying the time-to-live value
  131. * @exception IOException if an I/O exception occurs
  132. * while setting the time-to-live option.
  133. * @see #getTimeToLive()
  134. */
  135. protected abstract void setTimeToLive(int ttl) throws IOException;
  136. /**
  137. * Retrieve the TTL (time-to-live) option.
  138. * @exception IOException if an I/O exception occurs
  139. * while retrieving the time-to-live option
  140. * @return an <tt>int</tt> representing the time-to-live value
  141. * @see #setTimeToLive(int)
  142. */
  143. protected abstract int getTimeToLive() throws IOException;
  144. /**
  145. * Join the multicast group.
  146. * @param inetaddr multicast address to join.
  147. * @exception IOException if an I/O exception occurs
  148. * while joining the multicast group.
  149. */
  150. protected abstract void join(InetAddress inetaddr) throws IOException;
  151. /**
  152. * Leave the multicast group.
  153. * @param inetaddr multicast address to leave.
  154. * @exception IOException if an I/O exception occurs
  155. * while leaving the multicast group.
  156. */
  157. protected abstract void leave(InetAddress inetaddr) throws IOException;
  158. /**
  159. * Join the multicast group.
  160. * @param mcastaddr address to join.
  161. * @param netIf specifies the local interface to receive multicast
  162. * datagram packets
  163. * @throws IOException if an I/O exception occurs while joining
  164. * the multicast group
  165. * @since 1.4
  166. */
  167. protected abstract void joinGroup(SocketAddress mcastaddr,
  168. NetworkInterface netIf)
  169. throws IOException;
  170. /**
  171. * Leave the multicast group.
  172. * @param mcastaddr address to leave.
  173. * @param netIf specified the local interface to leave the group at
  174. * @throws IOException if an I/O exception occurs while leaving
  175. * the multicast group
  176. * @since 1.4
  177. */
  178. protected abstract void leaveGroup(SocketAddress mcastaddr,
  179. NetworkInterface netIf)
  180. throws IOException;
  181. /**
  182. * Close the socket.
  183. */
  184. protected abstract void close();
  185. /**
  186. * Gets the local port.
  187. * @return an <tt>int</tt> representing the local port value
  188. */
  189. protected int getLocalPort() {
  190. return localPort;
  191. }
  192. /**
  193. * Gets the datagram socket file descriptor.
  194. * @return a <tt>FileDescriptor</tt> object representing the datagram socket
  195. * file descriptor
  196. */
  197. protected FileDescriptor getFileDescriptor() {
  198. return fd;
  199. }
  200. }