|
Chapter 15 The java.net Package |
|
DatagramPacket
Name
DatagramPacket
- Class Name:
-
java.net.DatagramPacket
- Superclass:
-
java.lang.Object
- Immediate Subclasses:
-
None
- Interfaces Implemented:
-
None
- Availability:
-
JDK 1.0 or later
The DatagramPacket class represents
a packet of data that can be sent and received over the network using a
DatagramSocket. The class is
used to implement connectionless data communication.
public final class java.net.DatagramPacket extends java.lang.Object {
// Constructors
public DatagramPacket(byte[] ibuf, int ilength);
public DatagramPacket(byte[] ibuf, int ilength,
InetAddress iaddr, int iport);
// Instance Methods
public synchronized InetAddress getAddress();
public synchronized byte[] getData();
public synchronized int getLength();
public synchronized int getPort();
public synchronized void setAddress(InetAddress iaddr); // New in 1.1
public synchronized void setData(byte[] ibuf); // New in 1.1
public synchronized void setLength(int ilength); // New in 1.1
public synchronized void setPort(int iport); // New in 1.1
}
- Parameters
-
- ibuf
-
The data buffer for receiving incoming bytes.
- ilength
-
The number of bytes to read.
- Description
-
This constructor creates a DatagramPacket
that receives data. The value of ilength
must be less than or equal to ibuf.length.
This DatagramPacket can be
passed to DatagramSocket.receive().
- Parameters
-
- ibuf
-
The data buffer for the packet.
- ilength
-
The number of bytes to send.
- iaddr
-
The destination address.
- iport
-
The destination port number.
- Description
-
This constructor creates a DatagramPacket
that sends packets of length ilength
to the given port of the specified address. The value of ilength
must be less than or equal to ibuf.length.
The packets are sent using DatagramSocket.send().
- Returns
-
The IP address of the packet.
- Description
-
If this packet has been received, the method returns the address of the
machine that sent it. If the packet is being sent, the method returns the
destination address.
- Returns
-
The packet data.
- Description
-
This method returns the data buffer associated with this DatagramPacket
object. This data is either the data being sent or
the data that has been received.
- Returns
-
The packet length.
- Description
-
This method returns the length of the message in the buffer associated
with this DatagramPacket. This length
is either the length of the data being sent or the length of the data that
has been received.
- Returns
-
The port number of the packet.
- Description
-
If this packet has been received, the method returns the port number of
the machine that sent it. If the packet is being sent, the method returns
the destination port number.
- Availability
-
New as of JDK 1.1
- Parameters
-
- iaddr
-
The destination address for the packet.
- Description
-
This method sets the destination address for this packet. When the packet
is sent using DatagramSocket.send(),
it is sent to the specified address.
- Availability
-
New as of JDK 1.1
- Parameters
-
- ibuf
-
The data buffer for the packet.
- Description
-
This method sets the data for this packet. When the packet is sent using
DatagramSocket.send(), the
specified data is sent.
- Availability
-
New as of JDK 1.1
- Parameters
-
- ilength
-
The number of bytes to send.
- Description
-
This method sets the length of the data to be sent for this packet. When
the packet is sent using DatagramSocket.send(),
the specified amount of data is sent.
- Availability
-
New as of JDK 1.1
- Parameters
-
- iport
-
The port number for the packet.
- Description
-
This method sets the destination port number for this packet. When the
packet is sent using DatagramSocket.send(),
it is sent to the specified port.
DatagramSocket,
InetAddress
|
|