Wait for and return the next datagram. The Data is returned
as a Prolog term depending on Options. From is a
term of the format Ip:Port indicating the sender of the message. Here, Ip
is either an ip4 or ip6 structure. Socket can be waited for
using
wait_for_input/3. Defined Options:
- as(+Type)
- Defines the type for Data. Possible values are
atom
, codes
,
string
(default) or term
(parse as Prolog
term).
- encoding(+Encoding)
- Specify the encoding used to interpret the message. It is one of
octet
. iso_latin_1
, text
or utf8
.
- max_message_size(+Size)
- Specify the maximum number of bytes to read from a UDP datagram. Size
must be within the range 0-65535. If unspecified, a maximum of 4096
bytes will be read.
For example:
receive(Port) :-
udp_socket(Socket),
tcp_bind(Socket, Port),
repeat,
udp_receive(Socket, Data, From, [as(atom)]),
format('Got ~q from ~q~n', [Data, From]),
fail.