> For the complete documentation index, see [llms.txt](https://docs.servoy.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.servoy.com/reference/servoyextensions/server-plugins/udp/udpsocket.md).

# UDPSocket

## Methods Summarized

| Type                                                                     | Name                                                                                   | Summary                                                                                                         |
| ------------------------------------------------------------------------ | -------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
| void                                                                     | [close()](#close)                                                                      | Closes the socket and cleans up the resources.                                                                  |
| [JSPacket](/reference/servoyextensions/server-plugins/udp/jspacket.md)   | [getReceivedPacket()](#getreceivedpacket)                                              | Get a packet from receive buffer, read buffer until empty (null is returned).                                   |
| [Boolean](/reference/servoycore/dev-api/js-lib/boolean.md)               | [sendPacket(destIpOrHostname, packet)](#sendpacket-destiporhostname-packet)            | Send a packet, over this socket, no need to start it if you don't want to listen to incoming packages.          |
| [Boolean](/reference/servoycore/dev-api/js-lib/boolean.md)               | [sendPacket(destIpOrHostname, packet, port)](#sendpacket-destiporhostname-packet-port) | Send a packet, over this socket, no need to start it if you don't want to listen to incoming packages.          |
| [UDPSocket](/reference/servoyextensions/server-plugins/udp/udpsocket.md) | [start(packageReceivedCallback)](#start-packagereceivedcallback)                       | Starts the socket to listen for incoming packets, no need to call this method if you only want to send packets. |
| void                                                                     | [testPacket(packet)](#testpacket-packet)                                               | Put a test packet in the receive buffer to test your method call and getReceivedPacket.                         |

## Methods Detailed

### close()

Closes the socket and cleans up the resources.

**Returns:** void

### getReceivedPacket()

Get a packet from receive buffer, read buffer until empty (null is returned).

**Returns:** [JSPacket](/reference/servoyextensions/server-plugins/udp/jspacket.md) the next JSPacket from the receive buffer, or null if the buffer is empty.

**Sample**

```js
var packet = null
while( ( packet = socket.getReceivedPacket() ) != null)
{
	var text = packet.readUTF()
	var count = packet.readInt()
}
```

### sendPacket(destIpOrHostname, packet)

Send a packet, over this socket, no need to start it if you don't want to listen to incoming packages.\
This will send the packet to the same port as the socket itself is bound to.

**Parameters**

* [String](/reference/servoycore/dev-api/js-lib/string.md) **destIpOrHostname** the ip of the destination or the hostname
* [JSPacket](/reference/servoyextensions/server-plugins/udp/jspacket.md) **packet** the JSPacket to send

**Returns:** [Boolean](/reference/servoycore/dev-api/js-lib/boolean.md) true if the packet was successfully sent; otherwise, false.

**Sample**

```js
var socket = plugins.udp.createSocket(4321);
var packet = plugins.udp.createNewPacket();
packet.writeUTF('hello world!')
socket.sendPacket('10.0.0.1',packet)
socket.close();
```

### sendPacket(destIpOrHostname, packet, port)

Send a packet, over this socket, no need to start it if you don't want to listen to incoming packages.\
This will send the packet to the give port on the destination.

**Parameters**

* [String](/reference/servoycore/dev-api/js-lib/string.md) **destIpOrHostname** the ip of the destination or the hostname
* [JSPacket](/reference/servoyextensions/server-plugins/udp/jspacket.md) **packet** the JSPacket to send
* [Number](/reference/servoycore/dev-api/js-lib/number.md) **port** the port on which to send the packet

**Returns:** [Boolean](/reference/servoycore/dev-api/js-lib/boolean.md) true if the packet was successfully sent to the specified port; otherwise, false.

**Sample**

```js
var socket = plugins.udp.createSocket(4321);
var packet = plugins.udp.createNewPacket()
packet.writeUTF('hello world!')
socket.sendPacket('10.0.0.1',packet, 9999)
socket.close();
```

### start(packageReceivedCallback)

Starts the socket to listen for incoming packets, no need to call this method if you only want to send packets.\
the given function will be called when a packet is received, it will get as a parameter UPPSocket instance itself.

**Parameters**

* [Function](/reference/servoycore/dev-api/js-lib/function.md) **packageReceivedCallback** the callback function that will be called when a package is received, it will get as a parameter UPPSocket instance itself.

**Returns:** [UDPSocket](/reference/servoyextensions/server-plugins/udp/udpsocket.md) The UDPSocket instance, allowing method chaining after starting the listener.

**Sample**

```js
var socket = plugins.udp.createSocket(4321).start(callbackFunction);
function callbackFunction() {
  var string = socket.getReceivedPacket().readUTF();
  application.output(string);
}
```

### testPacket(packet)

Put a test packet in the receive buffer to test your method call and getReceivedPacket.

**Parameters**

* [JSPacket](/reference/servoyextensions/server-plugins/udp/jspacket.md) **packet** ;

**Returns:** void

**Sample**

```js
var packet = plugins.udp.createNewPacket()
packet.writeUTF('hello world!')
plugins.udp.testPacket(packet)
```

***


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.servoy.com/reference/servoyextensions/server-plugins/udp/udpsocket.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
