socket.udp()
Creates and returns an unconnected UDP object. Unconnected objects support the sendto, receive, receivefrom, 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.
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 the IP address and port number of the peer.
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 and a number with the port. 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: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.
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:
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.