|Did you know ...||Search Documentation:|
|UDP protocol support|
The socket library provides support for UDP sockets. The UDP protocol is a connection-less and unreliable datagram based protocol. That means that messages sent may or may not arrive at the client side, may arrive in a different order then they were sent or even may arrive multiple times. UDP messages are often used for data replication, streaming media, or service discovery.
library(udp_broadcast) provides a high-level
interface that builds on top of the
facilitating an unreliable
publish-subscribe pattern based communication channel. The
library(udp_broadcast) library supports multiple network
architectures, e.g., classical LAN broadcasting, multicast and
SOCK_DGRAMprotocol, ready for UDP connections.
term. The value
termcauses the data to be parsed to a Prolog string.
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.
as(Type)option of udp_receive/4. The are interpreted differently though. No Type corresponds to
CVT_ALLof PL_get_chars(). Using
CVT_ATOMand any of
codesis mapped to
CVT_STRING|CVT_LIST, allowing for a SWI-Prolog string object, list of character codes or list of characters. Finally,
CVT_WRITE_CANONICAL. This implies that arbitrary Prolog terms1write_canonical/2 does not support blobs nor cyclic terms can be sent reliably using the option list
[as(term),encoding(utf8)]), using the same option list for udp_receive/4.
A simple example to send UDP data is:
send(Host, Port, Message) :- udp_socket(S), udp_send(S, Message, Host:Port, ), tcp_close_socket(S).
A broadcast is achieved by using
prior to sending the datagram and using the local network broadcast
address as a ip/4
The normal mechanism to discover a service on the local network is for the client to send a broadcast message to an agreed port. The server receives this message and replies to the client with a message indicating further details to establish the communication.