LuaSocket: UDP support
Description: LuaSocket: The UDP support
Keywords: Lua, LuaSocket, Socket, UDP, Library, Network, Support

Network support for the Lua language

home · download · installation · introduction · reference

## UDP

socket.udp()

Creates and returns an unconnected IPv4 UDP object. Unconnected objects support the `sendto`, `receive`, `receivefrom`, `getoption`, `getsockname`, `setoption`, `settimeout`, `setpeername`, `setsockname`, and `close`. The `setpeername` is used to connect the object.

In case of success, a new unconnected UDP object returned. In case of error, `nil` is returned, followed by an error message.

socket.udp6()

Creates and returns an unconnected IPv6 UDP object. Unconnected objects support the `sendto`, `receive`, `receivefrom`, `getoption`, `getsockname`, `setoption`, `settimeout`, `setpeername`, `setsockname`, and `close`. The `setpeername` is used to connect the object.

In case of success, a new unconnected UDP object returned. In case of error, `nil` is returned, followed by an error message.

Note: The TCP object returned will have the option "`ipv6-v6only`" set to `true`.

connected:close()
unconnected:close()

Closes a UDP object. The internal socket used by the object is closed and the local address to which the object was bound is made available to other applications. No further operations (except for further calls to the `close` method) are allowed on a closed socket.

Note: It is important to close all used sockets once they are not needed, since, in many systems, each socket uses a file descriptor, which are limited system resources. Garbage-collected objects are automatically closed before destruction, though.

connected:getpeername()

Retrieves information about the peer associated with a connected UDP object.

Returns a string with the IP address of the peer, the port number that peer is using for the connection, and a string with the family ("`inet`" or "`inet6`"). In case of error, the method returns `nil`.

Note: It makes no sense to call this method on unconnected objects.

connected:getsockname()
unconnected:getsockname()

Returns the local address information associated to the object.

The method returns a string with local IP address, a number with the local port, and a string with the family ("`inet`" or "`inet6`"). In case of error, the method returns `nil`.

Note: UDP sockets are not bound to any address until the `setsockname` or the `sendto` method is called for the first time (in which case it is bound to an ephemeral port and the wild-card address).

connected:receive([size])
unconnected:receive([size])

Receives a datagram from the UDP object. If the UDP object is connected, only datagrams coming from the peer are accepted. Otherwise, the returned datagram can come from any host.

The optional `size` parameter specifies the maximum size of the datagram to be retrieved. If there are more than `size` bytes available in the datagram, the excess bytes are discarded. If there are less then `size` bytes available in the current datagram, the available bytes are returned. If `size` is omitted, the maximum datagram size is used (which is currently limited by the implementation to 8192 bytes).

In case of success, the method returns the received datagram. In case of timeout, the method returns `nil` followed by the string '`timeout`'.

unconnected:receivefrom([size])

Works exactly as the `receive` method, except it returns the IP address and port as extra return values (and is therefore slightly less efficient).

connected:getoption()
unconnected:getoption()

Gets an option value from the UDP object. See `setoption` for description of the option names and values.

`Option` is a string with the option name.

- '`dontroute`'
- '`broadcast`'
- '`reuseaddr`'
- '`reuseport`'
- '`ip-multicast-loop`'
- '`ipv6-v6only`'
- '`ip-multicast-if`'
- '`ip-multicast-ttl`'
- '`ip-add-membership`'
- '`ip-drop-membership`'

The method returns the option `value` in case of success, or `nil` followed by an error message otherwise.

connected:send(datagram)

Sends a datagram to the UDP peer of a connected object.

`Datagram` is a string with the datagram contents. The maximum datagram size for UDP is 64K minus IP layer overhead. However datagrams larger than the link layer packet size will be fragmented, which may deteriorate performance and/or reliability.

If successful, the method returns 1. In case of error, the method returns `nil` followed by an error message.

Note: In UDP, the `send` method never blocks and the only way it can fail is if the underlying transport layer refuses to send a message to the specified address (i.e. no interface accepts the address).

unconnected:sendto(datagram, ip, port)

Sends a datagram to the specified IP address and port number.

`Datagram` is a string with the datagram contents. The maximum datagram size for UDP is 64K minus IP layer overhead. However datagrams larger than the link layer packet size will be fragmented, which may deteriorate performance and/or reliability. `Ip` is the IP address of the recipient. Host names are not allowed for performance reasons. `Port` is the port number at the recipient.

If successful, the method returns 1. In case of error, the method returns `nil` followed by an error message.

Note: In UDP, the `send` method never blocks and the only way it can fail is if the underlying transport layer refuses to send a message to the specified address (i.e. no interface accepts the address).

connected:setpeername('*')
unconnected:setpeername(address, port)

Changes the peer of a UDP object. This method turns an unconnected UDP object into a connected UDP object or vice versa.

For connected objects, outgoing datagrams will be sent to the specified peer, and datagrams received from other peers will be discarded by the OS. Connected UDP objects must use the `send` and `receive` methods instead of `sendto` and `receivefrom`.

`Address` can be an IP address or a host name. `Port` is the port number. If `address` is '`*`' and the object is connected, the peer association is removed and the object becomes an unconnected object again. In that case, the `port` argument is ignored.

In case of error the method returns `nil` followed by an error message. In case of success, the method returns 1.

Note: Since the address of the peer does not have to be passed to and from the OS, the use of connected UDP objects is recommended when the same peer is used for several transmissions and can result in up to 30% performance gains.

Note: Starting with LuaSocket 2.1, the host name resolution depends on whether the socket was created by `socket.udp` or `socket.udp6`. Addresses from the appropriate family are tried in succession until the first success or until the last failure.

unconnected:setsockname(address, port)

Binds the UDP object to a local address.

`Address` can be an IP address or a host name. If `address` is '`*`' the system binds to all local interfaces using the constant `INADDR_ANY`. If `port` is 0, the system chooses an ephemeral port.

If successful, the method returns 1. In case of error, the method returns `nil` followed by an error message.

Note: This method can only be called before any datagram is sent through the UDP object, and only once. Otherwise, the system automatically binds the object to all local interfaces and chooses an ephemeral port as soon as the first datagram is sent. After the local address is set, either automatically by the system or explicitly by `setsockname`, it cannot be changed.

connected:setoption(option [, value])
unconnected:setoption(option [, value])

Sets options for the UDP object. Options are only needed by low-level or time-critical applications. You should only modify an option if you are sure you need it.

`Option` is a string with the option name, and `value` depends on the option being set:

- '`dontroute`': Indicates that outgoing messages should bypass the standard routing facilities. Receives a boolean value;
- '`broadcast`': Requests permission to send broadcast datagrams on the socket. Receives a boolean value;
- '`reuseaddr`': Indicates that the rules used in validating addresses supplied in a `bind()` call should allow reuse of local addresses. Receives a boolean value;
- '`reuseport`': Allows completely duplicate bindings by multiple processes if they all set '`reuseport`' before binding the port. Receives a boolean value;
- '`ip-multicast-loop`': Specifies whether or not a copy of an outgoing multicast datagram is delivered to the sending host as long as it is a member of the multicast group. Receives a boolean value;
- '`ipv6-v6only`': Specifies whether to restrict `inet6` sockets to sending and receiving only IPv6 packets. Receive a boolean value;
- '`ip-multicast-if`': Sets the interface over which outgoing multicast datagrams are sent. Receives an IP address;
- '`ip-multicast-ttl`': Sets the Time To Live in the IP header for outgoing multicast datagrams. Receives a number;
- '`ip-add-membership`': Joins the multicast group specified. Receives a table with fields `multiaddr` and `interface`, each containing an IP address;
- '`ip-drop-membership`': Leaves the multicast group specified. Receives a table with fields `multiaddr` and `interface`, each containing an IP address.

The method returns 1 in case of success, or `nil` followed by an error message otherwise.

Note: The descriptions above come from the man pages.

connected:settimeout(value)
unconnected:settimeout(value)

Changes the timeout values for the object. By default, the `receive` and `receivefrom` operations are blocking. That is, any call to the methods will block indefinitely, until data arrives. The `settimeout` function defines a limit on the amount of time the functions can block. When a timeout is set and the specified amount of time has elapsed, the affected methods give up and fail with an error code.

The amount of time to wait is specified as the `value` parameter, in seconds. The `nil` timeout `value` allows operations to block indefinitely. Negative timeout values have the same effect.

Note: In UDP, the `send` and `sendto` methods never block (the datagram is just passed to the OS and the call returns immediately). Therefore, the `settimeout` method has no effect on them.

Note: The old `timeout` method is deprecated. The name has been changed for sake of uniformity, since all other method names already contained verbs making their imperative nature obvious.