java.net
Class DatagramPacket

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.net.DatagramPacket

public final class DatagramPacket
extends Object sample code for java.lang.Object definition code for java.lang.Object

This class represents a datagram packet.

Datagram packets are used to implement a connectionless packet delivery service. Each message is routed from one machine to another based solely on information contained within that packet. Multiple packets sent from one machine to another might be routed differently, and might arrive in any order. Packet delivery is not guaranteed.

Since:
JDK1.0

Constructor Summary
DatagramPacket sample code for java.net.DatagramPacket.DatagramPacket(byte[], int) definition code for java.net.DatagramPacket.DatagramPacket(byte[], int) (byte[] buf, int length)
          Constructs a DatagramPacket for receiving packets of length length.
DatagramPacket sample code for java.net.DatagramPacket.DatagramPacket(byte[], int, java.net.InetAddress, int) definition code for java.net.DatagramPacket.DatagramPacket(byte[], int, java.net.InetAddress, int) (byte[] buf, int length, InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  address, int port)
          Constructs a datagram packet for sending packets of length length to the specified port number on the specified host.
DatagramPacket sample code for java.net.DatagramPacket.DatagramPacket(byte[], int, int) definition code for java.net.DatagramPacket.DatagramPacket(byte[], int, int) (byte[] buf, int offset, int length)
          Constructs a DatagramPacket for receiving packets of length length, specifying an offset into the buffer.
DatagramPacket sample code for java.net.DatagramPacket.DatagramPacket(byte[], int, int, java.net.InetAddress, int) definition code for java.net.DatagramPacket.DatagramPacket(byte[], int, int, java.net.InetAddress, int) (byte[] buf, int offset, int length, InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  address, int port)
          Constructs a datagram packet for sending packets of length length with offset ioffsetto the specified port number on the specified host.
DatagramPacket sample code for java.net.DatagramPacket.DatagramPacket(byte[], int, int, java.net.SocketAddress) definition code for java.net.DatagramPacket.DatagramPacket(byte[], int, int, java.net.SocketAddress) (byte[] buf, int offset, int length, SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  address)
          Constructs a datagram packet for sending packets of length length with offset ioffsetto the specified port number on the specified host.
DatagramPacket sample code for java.net.DatagramPacket.DatagramPacket(byte[], int, java.net.SocketAddress) definition code for java.net.DatagramPacket.DatagramPacket(byte[], int, java.net.SocketAddress) (byte[] buf, int length, SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  address)
          Constructs a datagram packet for sending packets of length length to the specified port number on the specified host.
 
Method Summary
 InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress getAddress sample code for java.net.DatagramPacket.getAddress() definition code for java.net.DatagramPacket.getAddress() ()
          Returns the IP address of the machine to which this datagram is being sent or from which the datagram was received.
 byte[] getData sample code for java.net.DatagramPacket.getData() definition code for java.net.DatagramPacket.getData() ()
          Returns the data buffer.
 int getLength sample code for java.net.DatagramPacket.getLength() definition code for java.net.DatagramPacket.getLength() ()
          Returns the length of the data to be sent or the length of the data received.
 int getOffset sample code for java.net.DatagramPacket.getOffset() definition code for java.net.DatagramPacket.getOffset() ()
          Returns the offset of the data to be sent or the offset of the data received.
 int getPort sample code for java.net.DatagramPacket.getPort() definition code for java.net.DatagramPacket.getPort() ()
          Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received.
 SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress getSocketAddress sample code for java.net.DatagramPacket.getSocketAddress() definition code for java.net.DatagramPacket.getSocketAddress() ()
          Gets the SocketAddress (usually IP address + port number) of the remote host that this packet is being sent to or is coming from.
 void setAddress sample code for java.net.DatagramPacket.setAddress(java.net.InetAddress) definition code for java.net.DatagramPacket.setAddress(java.net.InetAddress) (InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  iaddr)
          Sets the IP address of the machine to which this datagram is being sent.
 void setData sample code for java.net.DatagramPacket.setData(byte[]) definition code for java.net.DatagramPacket.setData(byte[]) (byte[] buf)
          Set the data buffer for this packet.
 void setData sample code for java.net.DatagramPacket.setData(byte[], int, int) definition code for java.net.DatagramPacket.setData(byte[], int, int) (byte[] buf, int offset, int length)
          Set the data buffer for this packet.
 void setLength sample code for java.net.DatagramPacket.setLength(int) definition code for java.net.DatagramPacket.setLength(int) (int length)
          Set the length for this packet.
 void setPort sample code for java.net.DatagramPacket.setPort(int) definition code for java.net.DatagramPacket.setPort(int) (int iport)
          Sets the port number on the remote host to which this datagram is being sent.
 void setSocketAddress sample code for java.net.DatagramPacket.setSocketAddress(java.net.SocketAddress) definition code for java.net.DatagramPacket.setSocketAddress(java.net.SocketAddress) (SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  address)
          Sets the SocketAddress (usually IP address + port number) of the remote host to which this datagram is being sent.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

DatagramPacket sample code for java.net.DatagramPacket(byte[], int, int) definition code for java.net.DatagramPacket(byte[], int, int)

public DatagramPacket(byte[] buf,
                      int offset,
                      int length)
Constructs a DatagramPacket for receiving packets of length length, specifying an offset into the buffer.

The length argument must be less than or equal to buf.length.

Parameters:
buf - buffer for holding the incoming datagram.
offset - the offset for the buffer
length - the number of bytes to read.
Since:
JDK1.2

DatagramPacket sample code for java.net.DatagramPacket(byte[], int) definition code for java.net.DatagramPacket(byte[], int)

public DatagramPacket(byte[] buf,
                      int length)
Constructs a DatagramPacket for receiving packets of length length.

The length argument must be less than or equal to buf.length.

Parameters:
buf - buffer for holding the incoming datagram.
length - the number of bytes to read.

DatagramPacket sample code for java.net.DatagramPacket(byte[], int, int, java.net.InetAddress, int) definition code for java.net.DatagramPacket(byte[], int, int, java.net.InetAddress, int)

public DatagramPacket(byte[] buf,
                      int offset,
                      int length,
                      InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  address,
                      int port)
Constructs a datagram packet for sending packets of length length with offset ioffsetto the specified port number on the specified host. The length argument must be less than or equal to buf.length.

Parameters:
buf - the packet data.
offset - the packet data offset.
length - the packet data length.
address - the destination address.
port - the destination port number.
Since:
JDK1.2
See Also:
InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress

DatagramPacket sample code for java.net.DatagramPacket(byte[], int, int, java.net.SocketAddress) definition code for java.net.DatagramPacket(byte[], int, int, java.net.SocketAddress)

public DatagramPacket(byte[] buf,
                      int offset,
                      int length,
                      SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  address)
               throws SocketException sample code for java.net.SocketException definition code for java.net.SocketException 
Constructs a datagram packet for sending packets of length length with offset ioffsetto the specified port number on the specified host. The length argument must be less than or equal to buf.length.

Parameters:
buf - the packet data.
offset - the packet data offset.
length - the packet data length.
address - the destination socket address.
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if address type is not supported
SocketException sample code for java.net.SocketException definition code for java.net.SocketException
Since:
1.4
See Also:
InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress

DatagramPacket sample code for java.net.DatagramPacket(byte[], int, java.net.InetAddress, int) definition code for java.net.DatagramPacket(byte[], int, java.net.InetAddress, int)

public DatagramPacket(byte[] buf,
                      int length,
                      InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  address,
                      int port)
Constructs a datagram packet for sending packets of length length to the specified port number on the specified host. The length argument must be less than or equal to buf.length.

Parameters:
buf - the packet data.
length - the packet length.
address - the destination address.
port - the destination port number.
See Also:
InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress

DatagramPacket sample code for java.net.DatagramPacket(byte[], int, java.net.SocketAddress) definition code for java.net.DatagramPacket(byte[], int, java.net.SocketAddress)

public DatagramPacket(byte[] buf,
                      int length,
                      SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  address)
               throws SocketException sample code for java.net.SocketException definition code for java.net.SocketException 
Constructs a datagram packet for sending packets of length length to the specified port number on the specified host. The length argument must be less than or equal to buf.length.

Parameters:
buf - the packet data.
length - the packet length.
address - the destination address.
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if address type is not supported
SocketException sample code for java.net.SocketException definition code for java.net.SocketException
Since:
1.4
See Also:
InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress
Method Detail

getAddress sample code for java.net.DatagramPacket.getAddress() definition code for java.net.DatagramPacket.getAddress()

public InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  getAddress()
Returns the IP address of the machine to which this datagram is being sent or from which the datagram was received.

Returns:
the IP address of the machine to which this datagram is being sent or from which the datagram was received.
See Also:
InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress , setAddress(java.net.InetAddress) sample code for java.net.DatagramPacket.setAddress(java.net.InetAddress) definition code for java.net.DatagramPacket.setAddress(java.net.InetAddress)

getPort sample code for java.net.DatagramPacket.getPort() definition code for java.net.DatagramPacket.getPort()

public int getPort()
Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received.

Returns:
the port number on the remote host to which this datagram is being sent or from which the datagram was received.
See Also:
setPort(int) sample code for java.net.DatagramPacket.setPort(int) definition code for java.net.DatagramPacket.setPort(int)

getData sample code for java.net.DatagramPacket.getData() definition code for java.net.DatagramPacket.getData()

public byte[] getData()
Returns the data buffer. The data received or the data to be sent starts from the offset in the buffer, and runs for length long.

Returns:
the buffer used to receive or send data
See Also:
setData(byte[], int, int) sample code for java.net.DatagramPacket.setData(byte[], int, int) definition code for java.net.DatagramPacket.setData(byte[], int, int)

getOffset sample code for java.net.DatagramPacket.getOffset() definition code for java.net.DatagramPacket.getOffset()

public int getOffset()
Returns the offset of the data to be sent or the offset of the data received.

Returns:
the offset of the data to be sent or the offset of the data received.
Since:
JDK1.2

getLength sample code for java.net.DatagramPacket.getLength() definition code for java.net.DatagramPacket.getLength()

public int getLength()
Returns the length of the data to be sent or the length of the data received.

Returns:
the length of the data to be sent or the length of the data received.
See Also:
setLength(int) sample code for java.net.DatagramPacket.setLength(int) definition code for java.net.DatagramPacket.setLength(int)

setData sample code for java.net.DatagramPacket.setData(byte[], int, int) definition code for java.net.DatagramPacket.setData(byte[], int, int)

public void setData(byte[] buf,
                    int offset,
                    int length)
Set the data buffer for this packet. This sets the data, length and offset of the packet.

Parameters:
buf - the buffer to set for this packet
offset - the offset into the data
length - the length of the data and/or the length of the buffer used to receive data
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if the argument is null
Since:
JDK1.2
See Also:
getData() sample code for java.net.DatagramPacket.getData() definition code for java.net.DatagramPacket.getData() , getOffset() sample code for java.net.DatagramPacket.getOffset() definition code for java.net.DatagramPacket.getOffset() , getLength() sample code for java.net.DatagramPacket.getLength() definition code for java.net.DatagramPacket.getLength()

setAddress sample code for java.net.DatagramPacket.setAddress(java.net.InetAddress) definition code for java.net.DatagramPacket.setAddress(java.net.InetAddress)

public void setAddress(InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  iaddr)
Sets the IP address of the machine to which this datagram is being sent.

Parameters:
iaddr - the InetAddress
Since:
JDK1.1
See Also:
getAddress() sample code for java.net.DatagramPacket.getAddress() definition code for java.net.DatagramPacket.getAddress()

setPort sample code for java.net.DatagramPacket.setPort(int) definition code for java.net.DatagramPacket.setPort(int)

public void setPort(int iport)
Sets the port number on the remote host to which this datagram is being sent.

Parameters:
iport - the port number
Since:
JDK1.1
See Also:
getPort() sample code for java.net.DatagramPacket.getPort() definition code for java.net.DatagramPacket.getPort()

setSocketAddress sample code for java.net.DatagramPacket.setSocketAddress(java.net.SocketAddress) definition code for java.net.DatagramPacket.setSocketAddress(java.net.SocketAddress)

public void setSocketAddress(SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  address)
Sets the SocketAddress (usually IP address + port number) of the remote host to which this datagram is being sent.

Parameters:
address - the SocketAddress
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if address is null or is a SocketAddress subclass not supported by this socket
Since:
1.4
See Also:
getSocketAddress() sample code for java.net.DatagramPacket.getSocketAddress() definition code for java.net.DatagramPacket.getSocketAddress()

getSocketAddress sample code for java.net.DatagramPacket.getSocketAddress() definition code for java.net.DatagramPacket.getSocketAddress()

public SocketAddress sample code for java.net.SocketAddress definition code for java.net.SocketAddress  getSocketAddress()
Gets the SocketAddress (usually IP address + port number) of the remote host that this packet is being sent to or is coming from.

Returns:
the SocketAddress
Since:
1.4
See Also:
setSocketAddress(java.net.SocketAddress) sample code for java.net.DatagramPacket.setSocketAddress(java.net.SocketAddress) definition code for java.net.DatagramPacket.setSocketAddress(java.net.SocketAddress)

setData sample code for java.net.DatagramPacket.setData(byte[]) definition code for java.net.DatagramPacket.setData(byte[])

public void setData(byte[] buf)
Set the data buffer for this packet. With the offset of this DatagramPacket set to 0, and the length set to the length of buf.

Parameters:
buf - the buffer to set for this packet.
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if the argument is null.
Since:
JDK1.1
See Also:
getLength() sample code for java.net.DatagramPacket.getLength() definition code for java.net.DatagramPacket.getLength() , getData() sample code for java.net.DatagramPacket.getData() definition code for java.net.DatagramPacket.getData()

setLength sample code for java.net.DatagramPacket.setLength(int) definition code for java.net.DatagramPacket.setLength(int)

public void setLength(int length)
Set the length for this packet. The length of the packet is the number of bytes from the packet's data buffer that will be sent, or the number of bytes of the packet's data buffer that will be used for receiving data. The length must be lesser or equal to the offset plus the length of the packet's buffer.

Parameters:
length - the length to set for this packet.
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the length is negative of if the length is greater than the packet's data buffer length.
Since:
JDK1.1
See Also:
getLength() sample code for java.net.DatagramPacket.getLength() definition code for java.net.DatagramPacket.getLength() , setData(byte[], int, int) sample code for java.net.DatagramPacket.setData(byte[], int, int) definition code for java.net.DatagramPacket.setData(byte[], int, int)