Java Reference
In-Depth Information
public void connect(InetAddress address, int port). Although UDP is a con-
nectionless protocol, this method has been added to the DatagramSocket
class, as of J2SE 1.4. Packets are still sent and received using the send() and
receive() methods, but connecting two datagram sockets improves delivery
performance since security checks only need to be performed once.
public void disconnect().
Disconnects any current connection.
DatagramPacket Class
Notice that the send() and receive() methods of the DatagramSocket class have
a DatagramPacket parameter. The DatagramPacket class represents a data-
gram packet, and (like DatagramSocket) it is used by both the sender and
receiver of a packet. The DatagramPacket class has six constructors: two for
receivers and four for senders.
The following two DatagramPacket constructors are used for receiving a
datagram packet:
public DatagramPacket(byte [] buffer, int length). Creates a datagram
packet for receiving a packet of the specified size. The buffer will contain
the incoming packet.
public DatagramPacket(byte [] buffer, int offset, int length). Same as
the previous constructor, except that the data of the incoming packet is
stored in the position of the byte array specified by the offset parameter.
The array of bytes passed in to these constructors is used to contain the data
of the incoming packet, and typically are empty arrays. If they are not empty,
then the incoming datagram packet overwrites the data in the array.
The following four constructors are used for sending a datagram packet:
public DatagramPacket(byte [] buffer, int length, InetAddress address,
int port). Creates a datagram packet for sending a packet of the speci-
fied size. The buffer contains the data of the packet, and the address and
port denote the recipient.
public DatagramPacket(byte [] buffer, int length, SocketAddress
address). Similar to the previous constructor, except that the name and
port number of the recipient are contained in a SocketAddress argument.
public DatagramPacket(byte [] buffer, int offset, int length, InetAddress
address, int port). Allows you to denote an offset into the array, which
(along with the length argument) determines a subset of the byte array
that represents the data.
public DatagramPacket(byte [] buffer, int offset, int length, SocketAddress
address). Similar to the previous constructor, except that the name and
port number of the recipient are contained in a SocketAddress argument.
Search WWH ::




Custom Search