Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.net

Class DatagramPacket

java.lang.Object
|
+--java.net.DatagramPacket


public final class DatagramPacket

extends Object

This class models a packet of data that is to be sent across the network using a connectionless protocol such as UDP. It contains the data to be send, as well as the destination address and port. Note that datagram packets can arrive in any order and are not guaranteed to be delivered at all.

This class can also be used for receiving data from the network.

Note that for all method below where the buffer length passed by the caller cannot exceed the actually length of the byte array passed as the buffer, if this condition is not true, then the method silently reduces the length value to maximum allowable value. Written using on-line Java Platform 1.2 API Specification, as well as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). Status: Believed complete and correct.

Authors:

Constructor Summary

DatagramPacket(byte[] buf, int offset, int length)

This method initializes a new instance of DatagramPacket which has the specified buffer, offset, and length.
DatagramPacket(byte[] buf, int length)

Initializes a new instance of DatagramPacket for receiving packets from the network.
DatagramPacket(byte[] buf, int offset, int length, java.net.InetAddress address, int port)

Initializes a new instance of DatagramPacket for transmitting packets across the network.
DatagramPacket(byte[] buf, int length, java.net.InetAddress address, int port)

Initializes a new instance of DatagramPacket for transmitting packets across the network.
DatagramPacket(byte[] buf, int offset, int length, java.net.SocketAddress address)

Initializes a new instance of DatagramPacket for transmitting packets across the network.
DatagramPacket(byte[] buf, int length, java.net.SocketAddress address)

Initializes a new instance of DatagramPacket for transmitting packets across the network.

Method Summary

synchronized java.net.InetAddressgetAddress()

Returns the address that this packet is being sent to or, if it was used to receive a packet, the address that is was received from.
synchronized byte[]getData()

Returns the data buffer for this packet
synchronized intgetLength()

Returns the length of the data in the buffer
synchronized intgetOffset()

This method returns the current offset value into the data buffer where data will be sent from.
synchronized intgetPort()

Returns the port number this packet is being sent to or, if it was used to receive a packet, the port that it was received from.
java.net.SocketAddressgetSocketAddress()

Gets the socket address of the host this packet will be sent to/is coming from
synchronized voidsetAddress(java.net.InetAddress iaddr)

This sets the address to which the data packet will be transmitted.
synchronized voidsetData(byte[] buf)

Sets the data buffer for this packet.
synchronized voidsetData(byte[] buf, int offset, int length)

This method sets the data buffer for the packet.
synchronized voidsetLength(int length)

Sets the length of the data in the buffer.
synchronized voidsetPort(int iport)

This sets the port to which the data packet will be transmitted.
voidsetSocketAddress(java.net.SocketAddress address)

Sets the address of the remote host this package will be sent

Constructor Details

DatagramPacket

public DatagramPacket(byte[] buf, int length)

Initializes a new instance of DatagramPacket for receiving packets from the network.

Parameters:


DatagramPacket

public DatagramPacket(byte[] buf, int offset, int length)

This method initializes a new instance of DatagramPacket which has the specified buffer, offset, and length.

Since:Parameters:


DatagramPacket

public DatagramPacket(byte[] buf, int offset, int length, java.net.InetAddress address, int port)

Initializes a new instance of DatagramPacket for transmitting packets across the network.

Since:Parameters:


DatagramPacket

public DatagramPacket(byte[] buf, int offset, int length, java.net.SocketAddress address)

Initializes a new instance of DatagramPacket for transmitting packets across the network.

Since:Parameters:

Throws:


DatagramPacket

public DatagramPacket(byte[] buf, int length, java.net.InetAddress address, int port)

Initializes a new instance of DatagramPacket for transmitting packets across the network.

Parameters:


DatagramPacket

public DatagramPacket(byte[] buf, int length, java.net.SocketAddress address)

Initializes a new instance of DatagramPacket for transmitting packets across the network.

Since:Parameters:

Throws:


Method Details

getAddress

public synchronized InetAddress getAddress()

Returns the address that this packet is being sent to or, if it was used to receive a packet, the address that is was received from. If the constructor that doesn not take an address was used to create this object and no packet was actually read into this object, then this method returns null.

Returns:


getData

public synchronized byte[] getData()

Returns the data buffer for this packet

Returns:


getLength

public synchronized int getLength()

Returns the length of the data in the buffer

Returns:


getOffset

public synchronized int getOffset()

This method returns the current offset value into the data buffer where data will be sent from.

Since:Returns:


getPort

public synchronized int getPort()

Returns the port number this packet is being sent to or, if it was used to receive a packet, the port that it was received from. If the constructor that doesn not take an address was used to create this object and no packet was actually read into this object, then this method will return 0.

Returns:


getSocketAddress

public SocketAddress getSocketAddress()

Gets the socket address of the host this packet will be sent to/is coming from

Since:Returns:


setAddress

public synchronized void setAddress(java.net.InetAddress iaddr)

This sets the address to which the data packet will be transmitted.

Since:Parameters:


setData

public synchronized void setData(byte[] buf)

Sets the data buffer for this packet.

Since:Parameters:

Throws:


setData

public synchronized void setData(byte[] buf, int offset, int length)

This method sets the data buffer for the packet.

Since:Parameters:

Throws:


setLength

public synchronized void setLength(int length)

Sets the length of the data in the buffer.

Since:Parameters:

Throws:


setPort

public synchronized void setPort(int iport)

This sets the port to which the data packet will be transmitted.

Since:Parameters:


setSocketAddress

public void setSocketAddress(java.net.SocketAddress address)

Sets the address of the remote host this package will be sent

Since:Parameters:

Throws: