Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/java/net/DatagramPacket#pubctors) \| [Methods](https://developer.android.com/reference/java/net/DatagramPacket#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/java/net/DatagramPacket#inhmethods)

# DatagramPacket


`
public

final

class
DatagramPacket
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | java.net.DatagramPacket |

<br />

*** ** * ** ***

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.

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/java/net/DatagramPacket#DatagramPacket(byte[],%20int)(byte[] buf, int length) ` Constructs a `DatagramPacket` for receiving packets of length `length`. |
| ` https://developer.android.com/reference/java/net/DatagramPacket#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. |
| ` https://developer.android.com/reference/java/net/DatagramPacket#DatagramPacket(byte[],%20int,%20int,%20java.net.InetAddress,%20int)(byte[] buf, int offset, int length, https://developer.android.com/reference/java/net/InetAddress 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. |
| ` https://developer.android.com/reference/java/net/DatagramPacket#DatagramPacket(byte[],%20int,%20int,%20java.net.SocketAddress)(byte[] buf, int offset, int length, https://developer.android.com/reference/java/net/SocketAddress address) ` Constructs a datagram packet for sending packets of length `length` with offset `ioffset`to the specified port number on the specified host. |
| ` https://developer.android.com/reference/java/net/DatagramPacket#DatagramPacket(byte[],%20int,%20java.net.InetAddress,%20int)(byte[] buf, int length, https://developer.android.com/reference/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. |
| ` https://developer.android.com/reference/java/net/DatagramPacket#DatagramPacket(byte[],%20int,%20java.net.SocketAddress)(byte[] buf, int length, https://developer.android.com/reference/java/net/SocketAddress address) ` Constructs a datagram packet for sending packets of length `length` to the specified port number on the specified host. |

| ### Public methods ||
|---|---|
| ` https://developer.android.com/reference/java/net/InetAddress` | ` https://developer.android.com/reference/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[]` | ` https://developer.android.com/reference/java/net/DatagramPacket#getData()() ` Returns the data buffer. |
| ` int` | ` https://developer.android.com/reference/java/net/DatagramPacket#getLength()() ` Returns the length of the data to be sent or the length of the data received. |
| ` int` | ` https://developer.android.com/reference/java/net/DatagramPacket#getOffset()() ` Returns the offset of the data to be sent or the offset of the data received. |
| ` int` | ` https://developer.android.com/reference/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. |
| ` https://developer.android.com/reference/java/net/SocketAddress` | ` https://developer.android.com/reference/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` | ` https://developer.android.com/reference/java/net/DatagramPacket#setAddress(java.net.InetAddress)(https://developer.android.com/reference/java/net/InetAddress iaddr) ` Sets the IP address of the machine to which this datagram is being sent. |
| ` void` | ` https://developer.android.com/reference/java/net/DatagramPacket#setData(byte[],%20int,%20int)(byte[] buf, int offset, int length) ` Set the data buffer for this packet. |
| ` void` | ` https://developer.android.com/reference/java/net/DatagramPacket#setData(byte[])(byte[] buf) ` Set the data buffer for this packet. |
| ` void` | ` https://developer.android.com/reference/java/net/DatagramPacket#setLength(int)(int length) ` Set the length for this packet. |
| ` void` | ` https://developer.android.com/reference/java/net/DatagramPacket#setPort(int)(int iport) ` Sets the port number on the remote host to which this datagram is being sent. |
| ` void` | ` https://developer.android.com/reference/java/net/DatagramPacket#setSocketAddress(java.net.SocketAddress)(https://developer.android.com/reference/java/net/SocketAddress address) ` Sets the SocketAddress (usually IP address + port number) of the remote host to which this datagram is being sent. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||

## Public constructors

### DatagramPacket

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

| Parameters ||
|---|---|
| `buf` | `byte`: buffer for holding the incoming datagram. <br /> |
| `length` | `int`: the number of bytes to read. <br /> |

### DatagramPacket

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

| Parameters ||
|---|---|
| `buf` | `byte`: buffer for holding the incoming datagram. <br /> |
| `offset` | `int`: the offset for the buffer <br /> |
| `length` | `int`: the number of bytes to read. <br /> |

### DatagramPacket

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public DatagramPacket (byte[] buf, 
                int offset, 
                int length, 
                InetAddress 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`.

<br />

| Parameters ||
|---|---|
| `buf` | `byte`: the packet data. <br /> |
| `offset` | `int`: the packet data offset. <br /> |
| `length` | `int`: the packet data length. <br /> |
| `address` | `InetAddress`: the destination address. <br /> |
| `port` | `int`: the destination port number. <br /> |

**See also:**

- `https://developer.android.com/reference/java/net/InetAddress`

### DatagramPacket

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public DatagramPacket (byte[] buf, 
                int offset, 
                int length, 
                SocketAddress 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.

<br />

| Parameters ||
|---|---|
| `buf` | `byte`: the packet data. <br /> |
| `offset` | `int`: the packet data offset. <br /> |
| `length` | `int`: the packet data length. <br /> |
| `address` | `SocketAddress`: the destination socket address. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if address type is not supported |

**See also:**

- `https://developer.android.com/reference/java/net/InetAddress`

### DatagramPacket

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public DatagramPacket (byte[] buf, 
                int length, 
                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`.


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

<br />

| Parameters ||
|---|---|
| `buf` | `byte`: the packet data. <br /> |
| `length` | `int`: the packet length. <br /> |
| `address` | `InetAddress`: the destination address. <br /> |
| `port` | `int`: the destination port number. <br /> |

**See also:**

- `https://developer.android.com/reference/java/net/InetAddress`

### DatagramPacket

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public DatagramPacket (byte[] buf, 
                int length, 
                SocketAddress 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`.

<br />

| Parameters ||
|---|---|
| `buf` | `byte`: the packet data. <br /> |
| `length` | `int`: the packet length. <br /> |
| `address` | `SocketAddress`: the destination address. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if address type is not supported |

**See also:**

- `https://developer.android.com/reference/java/net/InetAddress`

## Public methods

### getAddress

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public InetAddress getAddress ()
```

Returns the IP address of the machine to which this datagram is being
sent or from which the datagram was received.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/InetAddress` | the IP address of the machine to which this datagram is being sent or from which the datagram was received. <br /> |

**See also:**

- `https://developer.android.com/reference/java/net/InetAddress`
- `https://developer.android.com/reference/java/net/DatagramPacket#setAddress(java.net.InetAddress)`

### getData

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

| Returns ||
|---|---|
| `byte[]` | the buffer used to receive or send data <br /> |

**See also:**

- `https://developer.android.com/reference/java/net/DatagramPacket#setData(byte[],%20int,%20int)`

### getLength

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getLength ()
```

Returns the length of the data to be sent or the length of the
data received.

<br />

| Returns ||
|---|---|
| `int` | the length of the data to be sent or the length of the data received. <br /> |

**See also:**

- `https://developer.android.com/reference/java/net/DatagramPacket#setLength(int)`

### getOffset

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getOffset ()
```

Returns the offset of the data to be sent or the offset of the
data received.

<br />

| Returns ||
|---|---|
| `int` | the offset of the data to be sent or the offset of the data received. <br /> |

### getPort

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

| Returns ||
|---|---|
| `int` | the port number on the remote host to which this datagram is being sent or from which the datagram was received. <br /> |

**See also:**

- `https://developer.android.com/reference/java/net/DatagramPacket#setPort(int)`

### getSocketAddress

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public 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.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/SocketAddress` | the `SocketAddress` <br /> |

**See also:**

- `https://developer.android.com/reference/java/net/DatagramPacket#setSocketAddress(java.net.SocketAddress)`

### setAddress

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setAddress (InetAddress iaddr)
```

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

<br />

| Parameters ||
|---|---|
| `iaddr` | `InetAddress`: the `InetAddress` <br /> |

**See also:**

- `https://developer.android.com/reference/java/net/DatagramPacket#getAddress()`

### setData

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

| Parameters ||
|---|---|
| `buf` | `byte`: the buffer to set for this packet <br /> |
| `offset` | `int`: the offset into the data <br /> |
| `length` | `int`: the length of the data and/or the length of the buffer used to receive data <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if the argument is null |

**See also:**

- `https://developer.android.com/reference/java/net/DatagramPacket#getData()`
- `https://developer.android.com/reference/java/net/DatagramPacket#getOffset()`
- `https://developer.android.com/reference/java/net/DatagramPacket#getLength()`

### setData

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

| Parameters ||
|---|---|
| `buf` | `byte`: the buffer to set for this packet. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException` | if the argument is null. |

**See also:**

- `https://developer.android.com/reference/java/net/DatagramPacket#getLength()`
- `https://developer.android.com/reference/java/net/DatagramPacket#getData()`

### setLength

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

| Parameters ||
|---|---|
| `length` | `int`: the length to set for this packet. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the length is negative of if the length is greater than the packet's data buffer length. |

**See also:**

- `https://developer.android.com/reference/java/net/DatagramPacket#getLength()`
- `https://developer.android.com/reference/java/net/DatagramPacket#setData(byte[])`

### setPort

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setPort (int iport)
```

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

<br />

| Parameters ||
|---|---|
| `iport` | `int`: the port number <br /> |

**See also:**

- `https://developer.android.com/reference/java/net/DatagramPacket#getPort()`

### setSocketAddress

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setSocketAddress (SocketAddress address)
```

Sets the SocketAddress (usually IP address + port number) of the remote
host to which this datagram is being sent.

<br />

| Parameters ||
|---|---|
| `address` | `SocketAddress`: the `SocketAddress` <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if address is null or is a SocketAddress subclass not supported by this socket |

**See also:**

- `https://developer.android.com/reference/java/net/DatagramPacket#getSocketAddress()`