For the latest stable version, please use Emilua API 0.10!

ip.udp.socket

local sock = ip.udp.socket.new()
sock.open('v4')
sock:bind(ip.address.any_v4(), 1234)

local buf = byte_span.new(1024)
local nread, remote_addr, remote_port = sock:receive_from(buf)
sock:send_to(buf:slice(1, nread), remote_addr, remote_port)

Functions

new() → ip.udp.socket

Constructor.

open(self, address_family: "v4"|"v6"|ip.address)

Open the socket.

address_family can be either "v4" or "v6". If you provide an ip.address object, the appropriate value will be inferred.

bind(self, addr: ip.address|string, port: integer)

Bind the socket to the given local endpoint.

shutdown(self, what: "receive"|"send"|"both")

Disable sends or receives on the socket.

what can be one of the following:

"receive"

Shutdown the receive side of the socket.

"send"

Shutdown the send side of the socket.

"both"

Shutdown both send and receive on the socket.

Doing this only mutates the socket object, but nothing will be sent over the wire. It could be useful if you’re planning to send the FD around to other processes.

connect(self, addr: ip.address, port: integer)

Set the default destination address so datagrams can be sent using send() without specifying a destination address.

disconnect(self)

Dissolve the socket’s association by resetting the socket’s peer address (i.e. connect(3) will be called with an AF_UNSPEC address).

close(self)

Close the socket.

Any asynchronous send, receive or connect operations will be cancelled immediately, and will complete with the boost::asio::error::operation_aborted error.

cancel(self)

Cancel all asynchronous operations associated with the acceptor.

This function causes all outstanding asynchronous connect, send and receive operations to finish immediately, and the handlers for cancelled operations will be passed the boost::asio::error::operation_aborted error.

assign(self, address_family: "v4"|"v6"|ip.address, fd: file_descriptor)

Assign an existing native socket to self.

address_family can be either "v4" or "v6". If you provide an ip.address object, the appropriate value will be inferred.

release(self) → file_descriptor

Release ownership of the native descriptor implementation.

This function causes all outstanding asynchronous connect, send and receive operations to finish immediately, and the handlers for cancelled operations will be passed the boost::asio::error::operation_aborted error. Ownership of the native socket is then transferred to the caller.

receive(self, buffer: byte_span[, flags: string[]]) → integer

Receive a datagram and blocks current fiber until it completes or errs.

Returns the number of bytes read.

receive_from(self, buffer: byte_span[, flags: string[]]) → integer, ip.address, integer

Receive a datagram and blocks current fiber until it completes or errs.

Returns the number of bytes read plus the endpoint (address + port) of the remote sender of the datagram.

send(self, buffer: byte_span[, flags: string[]]) → integer

Send data on the datagram socket and blocks current fiber until it completes or errs.

Returns the number of bytes written.

The send operation can only be used with a connected socket. Use the send_to function to send data on an unconnected datagram socket.

send_to(self, buffer: byte_span, remote_addr: ip.address, remote_port: integer[, flags: string[]]) → integer

Send a datagram to the specified remote endpoint and blocks current fiber until it completes or errs.

Returns the number of bytes written.

set_option(self, opt: string, val)

Set an option on the socket.

Currently available options are:

get_option(self, opt: string) → value

Get an option from the socket.

Currently available options are:

io_control(self, command: string[, …​])

Perform an IO control command on the socket.

Currently available commands are:

"bytes_readable"

Expects no arguments. Get the amount of data that can be read without blocking. Implements the FIONREAD IO control command.

Function flags

do_not_route

Specify that the data should not be subject to routing.

end_of_record

Specifies that the data marks the end of a record.

out_of_band

Process out-of-band data.

peek

Peek at incoming data without removing it from the input queue.

Properties

is_open: boolean

Whether the socket is open.

local_address: ip.address

The local address endpoint of the socket.

local_port: integer

The local port endpoint of the socket.

remote_address: ip.address

The remote address endpoint of the socket.

remote_port: integer

The remote port endpoint of the socket.