1. /*
  2. * @(#)DatagramSocketImpl.java 1.30 03/01/23
  3. *
  4. * Copyright 2003 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. protected abstract void setTTL(byte ttl) throws IOException;
  116. /**
  117. * Retrieve the TTL (time-to-live) option.
  118. *
  119. * @exception IOException if an I/O exception occurs
  120. * while retrieving the time-to-live option
  121. * @deprecated use getTimeToLive instead.
  122. * @return a byte representing the TTL value
  123. * @see #setTTL(byte)
  124. */
  125. protected abstract byte getTTL() throws IOException;
  126. /**
  127. * Set the TTL (time-to-live) option.
  128. * @param ttl an <tt>int</tt> specifying the time-to-live value
  129. * @exception IOException if an I/O exception occurs
  130. * while setting the time-to-live option.
  131. * @see #getTimeToLive()
  132. */
  133. protected abstract void setTimeToLive(int ttl) throws IOException;
  134. /**
  135. * Retrieve the TTL (time-to-live) option.
  136. * @exception IOException if an I/O exception occurs
  137. * while retrieving the time-to-live option
  138. * @return an <tt>int</tt> representing the time-to-live value
  139. * @see #setTimeToLive(int)
  140. */
  141. protected abstract int getTimeToLive() throws IOException;
  142. /**
  143. * Join the multicast group.
  144. * @param inetaddr multicast address to join.
  145. * @exception IOException if an I/O exception occurs
  146. * while joining the multicast group.
  147. */
  148. protected abstract void join(InetAddress inetaddr) throws IOException;
  149. /**
  150. * Leave the multicast group.
  151. * @param inetaddr multicast address to leave.
  152. * @exception IOException if an I/O exception occurs
  153. * while leaving the multicast group.
  154. */
  155. protected abstract void leave(InetAddress inetaddr) throws IOException;
  156. /**
  157. * Join the multicast group.
  158. * @param mcastaddr address to join.
  159. * @param netIf specifies the local interface to receive multicast
  160. * datagram packets
  161. * @throws IOException if an I/O exception occurs while joining
  162. * the multicast group
  163. * @since 1.4
  164. */
  165. protected abstract void joinGroup(SocketAddress mcastaddr,
  166. NetworkInterface netIf)
  167. throws IOException;
  168. /**
  169. * Leave the multicast group.
  170. * @param mcastaddr address to leave.
  171. * @param netIf specified the local interface to leave the group at
  172. * @throws IOException if an I/O exception occurs while leaving
  173. * the multicast group
  174. * @since 1.4
  175. */
  176. protected abstract void leaveGroup(SocketAddress mcastaddr,
  177. NetworkInterface netIf)
  178. throws IOException;
  179. /**
  180. * Close the socket.
  181. */
  182. protected abstract void close();
  183. /**
  184. * Gets the local port.
  185. * @return an <tt>int</tt> representing the local port value
  186. */
  187. protected int getLocalPort() {
  188. return localPort;
  189. }
  190. /**
  191. * Gets the datagram socket file descriptor.
  192. * @return a <tt>FileDescriptor</tt> object representing the datagram socket
  193. * file descriptor
  194. */
  195. protected FileDescriptor getFileDescriptor() {
  196. return fd;
  197. }
  198. }