001 /*
002 * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.net;
027
028 import java.io.FileDescriptor;
029 import java.io.IOException;
030 import java.io.InterruptedIOException;
031
032 /**
033 * Abstract datagram and multicast socket implementation base class.
034 * @author Pavani Diwanji
035 * @since JDK1.1
036 */
037
038 public abstract class DatagramSocketImpl implements SocketOptions {
039
040 /**
041 * The local port number.
042 */
043 protected int localPort;
044
045 /**
046 * The file descriptor object.
047 */
048 protected FileDescriptor fd;
049
050 /**
051 * Creates a datagram socket.
052 * @exception SocketException if there is an error in the
053 * underlying protocol, such as a TCP error.
054 */
055 protected abstract void create() throws SocketException;
056
057 /**
058 * Binds a datagram socket to a local port and address.
059 * @param lport the local port
060 * @param laddr the local address
061 * @exception SocketException if there is an error in the
062 * underlying protocol, such as a TCP error.
063 */
064 protected abstract void bind(int lport, InetAddress laddr)
065 throws SocketException;
066
067 /**
068 * Sends a datagram packet. The packet contains the data and the
069 * destination address to send the packet to.
070 * @param p the packet to be sent.
071 * @exception IOException if an I/O exception occurs while sending the
072 * datagram packet.
073 * @exception PortUnreachableException may be thrown if the socket is connected
074 * to a currently unreachable destination. Note, there is no guarantee that
075 * the exception will be thrown.
076 */
077 protected abstract void send(DatagramPacket p) throws IOException;
078
079 /**
080 * Connects a datagram socket to a remote destination. This associates the remote
081 * address with the local socket so that datagrams may only be sent to this destination
082 * and received from this destination. This may be overridden to call a native
083 * system connect.
084 *
085 * <p>If the remote destination to which the socket is connected does not
086 * exist, or is otherwise unreachable, and if an ICMP destination unreachable
087 * packet has been received for that address, then a subsequent call to
088 * send or receive may throw a PortUnreachableException.
089 * Note, there is no guarantee that the exception will be thrown.
090 * @param address the remote InetAddress to connect to
091 * @param port the remote port number
092 * @exception SocketException may be thrown if the socket cannot be
093 * connected to the remote destination
094 * @since 1.4
095 */
096 protected void connect(InetAddress address, int port)
097 throws SocketException {
098 }
099
100 /**
101 * Disconnects a datagram socket from its remote destination.
102 * @since 1.4
103 */
104 protected void disconnect() {
105 }
106
107 /**
108 * Peek at the packet to see who it is from. Updates the specified <code>InetAddress</code>
109 * to the address which the packet came from.
110 * @param i an InetAddress object
111 * @return the port number which the packet came from.
112 * @exception IOException if an I/O exception occurs
113 * @exception PortUnreachableException may be thrown if the socket is connected
114 * to a currently unreachable destination. Note, there is no guarantee that the
115 * exception will be thrown.
116 */
117 protected abstract int peek(InetAddress i) throws IOException;
118
119 /**
120 * Peek at the packet to see who it is from. The data is copied into the specified
121 * <code>DatagramPacket</code>. The data is returned,
122 * but not consumed, so that a subsequent peekData/receive operation
123 * will see the same data.
124 * @param p the Packet Received.
125 * @return the port number which the packet came from.
126 * @exception IOException if an I/O exception occurs
127 * @exception PortUnreachableException may be thrown if the socket is connected
128 * to a currently unreachable destination. Note, there is no guarantee that the
129 * exception will be thrown.
130 * @since 1.4
131 */
132 protected abstract int peekData(DatagramPacket p)
133 throws IOException;
134
135 /**
136 * Receive the datagram packet.
137 * @param p the Packet Received.
138 * @exception IOException if an I/O exception occurs
139 * while receiving the datagram packet.
140 * @exception PortUnreachableException may be thrown if the socket is connected
141 * to a currently unreachable destination. Note, there is no guarantee that the
142 * exception will be thrown.
143 */
144 protected abstract void receive(DatagramPacket p)
145 throws IOException;
146
147 /**
148 * Set the TTL (time-to-live) option.
149 * @param ttl a byte specifying the TTL value
150 *
151 * @deprecated use setTimeToLive instead.
152 * @exception IOException if an I/O exception occurs while setting
153 * the time-to-live option.
154 * @see #getTTL()
155 */
156 @Deprecated
157 protected abstract void setTTL(byte ttl) throws IOException;
158
159 /**
160 * Retrieve the TTL (time-to-live) option.
161 *
162 * @exception IOException if an I/O exception occurs
163 * while retrieving the time-to-live option
164 * @deprecated use getTimeToLive instead.
165 * @return a byte representing the TTL value
166 * @see #setTTL(byte)
167 */
168 @Deprecated
169 protected abstract byte getTTL() throws IOException;
170
171 /**
172 * Set the TTL (time-to-live) option.
173 * @param ttl an <tt>int</tt> specifying the time-to-live value
174 * @exception IOException if an I/O exception occurs
175 * while setting the time-to-live option.
176 * @see #getTimeToLive()
177 */
178 protected abstract void setTimeToLive(int ttl) throws IOException;
179
180 /**
181 * Retrieve the TTL (time-to-live) option.
182 * @exception IOException if an I/O exception occurs
183 * while retrieving the time-to-live option
184 * @return an <tt>int</tt> representing the time-to-live value
185 * @see #setTimeToLive(int)
186 */
187 protected abstract int getTimeToLive() throws IOException;
188
189 /**
190 * Join the multicast group.
191 * @param inetaddr multicast address to join.
192 * @exception IOException if an I/O exception occurs
193 * while joining the multicast group.
194 */
195 protected abstract void join(InetAddress inetaddr)
196 throws IOException;
197
198 /**
199 * Leave the multicast group.
200 * @param inetaddr multicast address to leave.
201 * @exception IOException if an I/O exception occurs
202 * while leaving the multicast group.
203 */
204 protected abstract void leave(InetAddress inetaddr)
205 throws IOException;
206
207 /**
208 * Join the multicast group.
209 * @param mcastaddr address to join.
210 * @param netIf specifies the local interface to receive multicast
211 * datagram packets
212 * @throws IOException if an I/O exception occurs while joining
213 * the multicast group
214 * @since 1.4
215 */
216 protected abstract void joinGroup(SocketAddress mcastaddr,
217 NetworkInterface netIf) throws IOException;
218
219 /**
220 * Leave the multicast group.
221 * @param mcastaddr address to leave.
222 * @param netIf specified the local interface to leave the group at
223 * @throws IOException if an I/O exception occurs while leaving
224 * the multicast group
225 * @since 1.4
226 */
227 protected abstract void leaveGroup(SocketAddress mcastaddr,
228 NetworkInterface netIf) throws IOException;
229
230 /**
231 * Close the socket.
232 */
233 protected abstract void close();
234
235 /**
236 * Gets the local port.
237 * @return an <tt>int</tt> representing the local port value
238 */
239 protected int getLocalPort() {
240 return localPort;
241 }
242
243 /**
244 * Gets the datagram socket file descriptor.
245 * @return a <tt>FileDescriptor</tt> object representing the datagram socket
246 * file descriptor
247 */
248 protected FileDescriptor getFileDescriptor() {
249 return fd;
250 }
251 }
|