# UDPSocket

## Methods Summarized

| Type                                                                                         | Name                                                                                   | Summary                                                                                                         |
| -------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
| void                                                                                         | [close()](#close)                                                                      | Closes the socket and cleans up the resources.                                                                  |
| [JSPacket](https://docs.servoy.com/reference/servoyextensions/server-plugins/udp/jspacket)   | [getReceivedPacket()](#getreceivedpacket)                                              | Get a packet from receive buffer, read buffer until empty (null is returned).                                   |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)               | [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](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)               | [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](https://docs.servoy.com/reference/servoyextensions/server-plugins/udp/udpsocket) | [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](https://docs.servoy.com/reference/servoyextensions/server-plugins/udp/jspacket) 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](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **destIpOrHostname** the ip of the destination or the hostname
* [JSPacket](https://docs.servoy.com/reference/servoyextensions/server-plugins/udp/jspacket) **packet** the JSPacket to send

**Returns:** [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) 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](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **destIpOrHostname** the ip of the destination or the hostname
* [JSPacket](https://docs.servoy.com/reference/servoyextensions/server-plugins/udp/jspacket) **packet** the JSPacket to send
* [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) **port** the port on which to send the packet

**Returns:** [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) 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](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/function) **packageReceivedCallback** the callback function that will be called when a package is received, it will get as a parameter UPPSocket instance itself.

**Returns:** [UDPSocket](https://docs.servoy.com/reference/servoyextensions/server-plugins/udp/udpsocket) 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](https://docs.servoy.com/reference/servoyextensions/server-plugins/udp/jspacket) **packet** ;

**Returns:** void

**Sample**

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

***
