6.5 Socket predicate reference
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • SWI-Prolog C-library
        • library(socket): Network socket (TCP and UDP) library
          • Socket predicate reference
            • socket_create/2
            • tcp_socket/1
            • unix_domain_socket/1
            • tcp_close_socket/1
            • tcp_open_socket/2
            • tcp_open_socket/3
            • tcp_bind/2
            • tcp_listen/2
            • tcp_accept/3
            • tcp_connect/2
            • rewrite_host/3
            • tcp_connect/4
            • tcp_connect/3
            • tcp_select/3
            • try_proxy/4
            • proxy_for_url/3
            • udp_socket/1
            • udp_receive/4
            • udp_send/4
            • tcp_setopt/2
            • tcp_fcntl/3
            • tcp_getopt/2
            • host_address/3
            • tcp_host_to_address/2
            • gethostname/1
            • ip_name/2
            • negotiate_socks_connection/2
Availability::- use_module(library(socket)).(can be autoloaded)
Source[det]udp_receive(+Socket, -Data, -From, +Options)
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.