# DatagramPacket

public final class **DatagramPacket** extends [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)  
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.

### Public Constructor Summary

|---|---|
|   | [DatagramPacket](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#DatagramPacket(byte[],%20int,%20int))(byte\[\] buf, int offset, int length) Constructs a `DatagramPacket` for receiving packets of length `length`, specifying an offset into the buffer. |
|   | [DatagramPacket](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#DatagramPacket(byte[],%20int))(byte\[\] buf, int length) Constructs a `DatagramPacket` for receiving packets of length `length`. |
|   | [DatagramPacket](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#DatagramPacket(byte[],%20int,%20int,%20java.net.InetAddress,%20int))(byte\[\] buf, int offset, int length, [InetAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/InetAddress.html) address, int port) Constructs a datagram packet for sending packets of length `length` with offset `ioffset`to the specified port number on the specified host. |
|   | [DatagramPacket](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#DatagramPacket(byte[],%20int,%20int,%20java.net.SocketAddress))(byte\[\] buf, int offset, int length, [SocketAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/SocketAddress.html) address) Constructs a datagram packet for sending packets of length `length` with offset `ioffset`to the specified port number on the specified host. |
|   | [DatagramPacket](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#DatagramPacket(byte[],%20int,%20java.net.InetAddress,%20int))(byte\[\] buf, int length, [InetAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/InetAddress.html) address, int port) Constructs a datagram packet for sending packets of length `length` to the specified port number on the specified host. |
|   | [DatagramPacket](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#DatagramPacket(byte[],%20int,%20java.net.SocketAddress))(byte\[\] buf, int length, [SocketAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/SocketAddress.html) address) Constructs a datagram packet for sending packets of length `length` to the specified port number on the specified host. |

### Public Method Summary

|---|---|
| synchronized [InetAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/InetAddress.html) | [getAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#getAddress())() Returns the IP address of the machine to which this datagram is being sent or from which the datagram was received. |
| synchronized byte\[\] | [getData](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#getData())() Returns the data buffer. |
| synchronized int | [getLength](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#getLength())() Returns the length of the data to be sent or the length of the data received. |
| synchronized int | [getOffset](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#getOffset())() Returns the offset of the data to be sent or the offset of the data received. |
| synchronized int | [getPort](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#getPort())() Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received. |
| synchronized [SocketAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/SocketAddress.html) | [getSocketAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#getSocketAddress())() Gets the SocketAddress (usually IP address + port number) of the remote host that this packet is being sent to or is coming from. |
| synchronized void | [setAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#setAddress(java.net.InetAddress))([InetAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/InetAddress.html) iaddr) Sets the IP address of the machine to which this datagram is being sent. |
| synchronized void | [setData](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#setData(byte[],%20int,%20int))(byte\[\] buf, int offset, int length) Set the data buffer for this packet. |
| synchronized void | [setData](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#setData(byte[]))(byte\[\] buf) Set the data buffer for this packet. |
| synchronized void | [setLength](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#setLength(int))(int length) Set the length for this packet. |
| synchronized void | [setPort](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#setPort(int))(int iport) Sets the port number on the remote host to which this datagram is being sent. |
| synchronized void | [setSocketAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#setSocketAddress(java.net.SocketAddress))([SocketAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/SocketAddress.html) address) Sets the SocketAddress (usually IP address + port number) of the remote host to which this datagram is being sent. |

### Inherited Method Summary

From class [java.lang.Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)

|---|---|
| [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |
| final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\> | [getClass](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#getClass())() Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this object's class. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |
| final void | [notify](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| final void | [notifyAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |

## Public Constructors

#### 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. |
|---|---|

#### 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. |
|---|---|

#### public
**DatagramPacket**
(byte\[\] buf, int offset, int length, [InetAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/InetAddress.html) address, int port)

Constructs a datagram packet for sending packets of length
`length` with offset `ioffset`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. |
| offset | the packet data offset. |
| length | the packet data length. |
| address | the destination address. |
| port | the destination port number. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/InetAddress.html`

#### public
**DatagramPacket**
(byte\[\] buf, int offset, int length, [SocketAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/SocketAddress.html) address)

Constructs a datagram packet for sending packets of length
`length` with offset `ioffset`to the
specified port number on the specified host. The
`length` argument must be less than or equal to
`buf.length`.


*Android note*: Up to and including API 25 this method declared that a SocketException
can be thrown, although the exception is never thrown. Code compiled against a newer SDK does
not need to catch the exception and will be binary compatible with older versions of Android.

##### Parameters

| buf | the packet data. |
| offset | the packet data offset. |
| length | the packet data length. |
| address | the destination socket address. |
|---|---|

##### Throws

| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if address type is not supported |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/InetAddress.html`

#### public
**DatagramPacket**
(byte\[\] buf, int length, [InetAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/InetAddress.html) 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`.


*Android note*: Up to and including API 25 this method declared that a SocketException
can be thrown, although the exception is never thrown. Code compiled against a newer SDK does
not need to catch the exception and will be binary compatible with older versions of Android.

##### Parameters

| buf | the packet data. |
| length | the packet length. |
| address | the destination address. |
| port | the destination port number. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/InetAddress.html`

#### public
**DatagramPacket**
(byte\[\] buf, int length, [SocketAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/SocketAddress.html) address)

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](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if address type is not supported |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/InetAddress.html`

## Public Methods

#### public synchronized [InetAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/InetAddress.html)
**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

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/InetAddress.html`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#setAddress(java.net.InetAddress)`

#### public synchronized 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

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#setData(byte[],%20int,%20int)`

#### public synchronized 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

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#setLength(int)`

#### public synchronized 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.

#### public synchronized 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

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#setPort(int)`

#### public synchronized [SocketAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/SocketAddress.html)
**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`

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#setSocketAddress(java.net.SocketAddress)`

#### public synchronized void
**setAddress**
([InetAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/InetAddress.html) iaddr)

Sets the IP address of the machine to which this datagram
is being sent.

##### Parameters

| iaddr | the `InetAddress` |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#getAddress()`

#### public synchronized 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](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if the argument is null |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#getData()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#getOffset()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#getLength()`

#### public synchronized 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](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/NullPointerException.html) | if the argument is null. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#getLength()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#getData()`

#### public synchronized 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](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if the length is negative of if the length is greater than the packet's data buffer length. |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#getLength()`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#setData(byte[])`

#### public synchronized void
**setPort**
(int iport)

Sets the port number on the remote host to which this datagram
is being sent.

##### Parameters

| iport | the port number |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#getPort()`

#### public synchronized void
**setSocketAddress**
([SocketAddress](https://developers.google.com/j2objc/javadoc/jre/reference/java/net/SocketAddress.html) 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](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | if address is null or is a SocketAddress subclass not supported by this socket |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/net/DatagramPacket.html#getSocketAddress()`