6.6 UDP protocol support
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • SWI-Prolog C-library
        • library(socket): Network socket (TCP and UDP) library
          • UDP protocol support
            • udp_socket/1
            • udp_receive/4
            • udp_send/4
Availability::- use_module(library(socket)).(can be autoloaded)
Sourceudp_receive(+Socket, -Data, -From, +Options)
Wait for and return the next datagram. The data is returned as a Prolog string object (see string_to_list/2). From is a term of the format ip(A,B,C,D):Port indicating the sender of the message. Socket can be waited for using wait_for_input/3. Defined Options:
as(+Type)
Defines the returned term-type. Type is one of atom, codes, string (default) or term. The value term causes the data to be parsed to a Prolog string.
encoding(+Encoding)
Specify the encoding used to intepret 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.

The typical sequence to receive UDP data is:

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.