KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > net > DatagramSocketImpl


1 /*
2  * @(#)DatagramSocketImpl.java 1.34 06/04/26
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.net;
9
10 import java.io.FileDescriptor JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InterruptedIOException JavaDoc;
13
14 /**
15  * Abstract datagram and multicast socket implementation base class.
16  * @author Pavani Diwanji
17  * @since JDK1.1
18  */

19
20 public abstract class DatagramSocketImpl implements SocketOptions JavaDoc {
21
22     /**
23      * The local port number.
24      */

25     protected int localPort;
26
27     /**
28      * The file descriptor object.
29      */

30     protected FileDescriptor JavaDoc fd;
31
32     /**
33      * Creates a datagram socket.
34      * @exception SocketException if there is an error in the
35      * underlying protocol, such as a TCP error.
36      */

37     protected abstract void create() throws SocketException JavaDoc;
38
39     /**
40      * Binds a datagram socket to a local port and address.
41      * @param lport the local port
42      * @param laddr the local address
43      * @exception SocketException if there is an error in the
44      * underlying protocol, such as a TCP error.
45      */

46     protected abstract void bind(int lport, InetAddress JavaDoc laddr) throws SocketException JavaDoc;
47
48     /**
49      * Sends a datagram packet. The packet contains the data and the
50      * destination address to send the packet to.
51      * @param p the packet to be sent.
52      * @exception IOException if an I/O exception occurs while sending the
53      * datagram packet.
54      * @exception PortUnreachableException may be thrown if the socket is connected
55      * to a currently unreachable destination. Note, there is no guarantee that
56      * the exception will be thrown.
57      */

58     protected abstract void send(DatagramPacket JavaDoc p) throws IOException JavaDoc;
59
60     /**
61      * Connects a datagram socket to a remote destination. This associates the remote
62      * address with the local socket so that datagrams may only be sent to this destination
63      * and received from this destination. This may be overridden to call a native
64      * system connect.
65      *
66      * <p>If the remote destination to which the socket is connected does not
67      * exist, or is otherwise unreachable, and if an ICMP destination unreachable
68      * packet has been received for that address, then a subsequent call to
69      * send or receive may throw a PortUnreachableException.
70      * Note, there is no guarantee that the exception will be thrown.
71      * @param address the remote InetAddress to connect to
72      * @param port the remote port number
73      * @exception SocketException may be thrown if the socket cannot be
74      * connected to the remote destination
75      * @since 1.4
76      */

77     protected void connect(InetAddress JavaDoc address, int port) throws SocketException JavaDoc {}
78
79     /**
80      * Disconnects a datagram socket from its remote destination.
81      * @since 1.4
82      */

83     protected void disconnect() {}
84
85     /**
86      * Peek at the packet to see who it is from. Updates the specified <code>InetAddress</code>
87      * to the address which the packet came from.
88      * @param i an InetAddress object
89      * @return the port number which the packet came from.
90      * @exception IOException if an I/O exception occurs
91      * @exception PortUnreachableException may be thrown if the socket is connected
92      * to a currently unreachable destination. Note, there is no guarantee that the
93      * exception will be thrown.
94      */

95     protected abstract int peek(InetAddress JavaDoc i) throws IOException JavaDoc;
96
97     /**
98      * Peek at the packet to see who it is from. The data is copied into the specified
99      * <code>DatagramPacket</code>. The data is returned,
100      * but not consumed, so that a subsequent peekData/receive operation
101      * will see the same data.
102      * @param p the Packet Received.
103      * @return the port number which the packet came from.
104      * @exception IOException if an I/O exception occurs
105      * @exception PortUnreachableException may be thrown if the socket is connected
106      * to a currently unreachable destination. Note, there is no guarantee that the
107      * exception will be thrown.
108      * @since 1.4
109      */

110     protected abstract int peekData(DatagramPacket JavaDoc p) throws IOException JavaDoc;
111     /**
112      * Receive the datagram packet.
113      * @param p the Packet Received.
114      * @exception IOException if an I/O exception occurs
115      * while receiving the datagram packet.
116      * @exception PortUnreachableException may be thrown if the socket is connected
117      * to a currently unreachable destination. Note, there is no guarantee that the
118      * exception will be thrown.
119      */

120     protected abstract void receive(DatagramPacket JavaDoc p) throws IOException JavaDoc;
121
122     /**
123      * Set the TTL (time-to-live) option.
124      * @param ttl a byte specifying the TTL value
125      *
126      * @deprecated use setTimeToLive instead.
127      * @exception IOException if an I/O exception occurs while setting
128      * the time-to-live option.
129      * @see #getTTL()
130      */

131     @Deprecated JavaDoc
132     protected abstract void setTTL(byte ttl) throws IOException JavaDoc;
133
134     /**
135      * Retrieve the TTL (time-to-live) option.
136      *
137      * @exception IOException if an I/O exception occurs
138      * while retrieving the time-to-live option
139      * @deprecated use getTimeToLive instead.
140      * @return a byte representing the TTL value
141      * @see #setTTL(byte)
142      */

143     @Deprecated JavaDoc
144     protected abstract byte getTTL() throws IOException JavaDoc;
145
146     /**
147      * Set the TTL (time-to-live) option.
148      * @param ttl an <tt>int</tt> specifying the time-to-live value
149      * @exception IOException if an I/O exception occurs
150      * while setting the time-to-live option.
151      * @see #getTimeToLive()
152      */

153     protected abstract void setTimeToLive(int ttl) throws IOException JavaDoc;
154
155     /**
156      * Retrieve the TTL (time-to-live) option.
157      * @exception IOException if an I/O exception occurs
158      * while retrieving the time-to-live option
159      * @return an <tt>int</tt> representing the time-to-live value
160      * @see #setTimeToLive(int)
161      */

162     protected abstract int getTimeToLive() throws IOException JavaDoc;
163
164     /**
165      * Join the multicast group.
166      * @param inetaddr multicast address to join.
167      * @exception IOException if an I/O exception occurs
168      * while joining the multicast group.
169      */

170     protected abstract void join(InetAddress JavaDoc inetaddr) throws IOException JavaDoc;
171
172     /**
173      * Leave the multicast group.
174      * @param inetaddr multicast address to leave.
175      * @exception IOException if an I/O exception occurs
176      * while leaving the multicast group.
177      */

178     protected abstract void leave(InetAddress JavaDoc inetaddr) throws IOException JavaDoc;
179
180     /**
181      * Join the multicast group.
182      * @param mcastaddr address to join.
183      * @param netIf specifies the local interface to receive multicast
184      * datagram packets
185      * @throws IOException if an I/O exception occurs while joining
186      * the multicast group
187      * @since 1.4
188      */

189     protected abstract void joinGroup(SocketAddress JavaDoc mcastaddr,
190                       NetworkInterface JavaDoc netIf)
191     throws IOException JavaDoc;
192
193     /**
194      * Leave the multicast group.
195      * @param mcastaddr address to leave.
196      * @param netIf specified the local interface to leave the group at
197      * @throws IOException if an I/O exception occurs while leaving
198      * the multicast group
199      * @since 1.4
200      */

201     protected abstract void leaveGroup(SocketAddress JavaDoc mcastaddr,
202                        NetworkInterface JavaDoc netIf)
203     throws IOException JavaDoc;
204
205     /**
206      * Close the socket.
207      */

208     protected abstract void close();
209
210     /**
211      * Gets the local port.
212      * @return an <tt>int</tt> representing the local port value
213      */

214     protected int getLocalPort() {
215     return localPort;
216     }
217
218     /**
219      * Gets the datagram socket file descriptor.
220      * @return a <tt>FileDescriptor</tt> object representing the datagram socket
221      * file descriptor
222      */

223     protected FileDescriptor JavaDoc getFileDescriptor() {
224     return fd;
225     }
226 }
227
Popular Tags