next up previous index
Next: Stream Connection Between Up: Interprocess Communication Previous: Stream Connection on

Datagram Connection on a Single Machine

The datagram connection is based on packets sent from one process to another, similar to usual LAN's, e.g. the Ethernet. The communication protocol does not guarantee that the message will always be delivered, but normally it will be. Every packet represents a message which is read separately at the system level, however at the Prolog level the packet boundaries are not visiblegif. The difference to stream communication is that there is no obligatory connection between the server and the client. First the socket has to be created, and then the process which wants to read from the it binds the socket to a name. Any other process can then connect directly to this socket using the connect/2 predicate and send data there. This connection can be temporary, and after writing the message to the socket the process can connect it to another socket, or just disconnect it by calling connect(Socket, 0).

Such datagram connection works only in one direction, namely from the process that called connect/2 to the process that called bind/2, however the connection in the other direction can be established in the same way:

server:
    % Make a named socket and read two terms from it
    [eclipse 10]: socket(unix, datagram, s), bind(s, '/tmp/sock').

    yes.
    [eclipse 11]: read(s, X), read(s, Y).

process1:
    % Connect a socket to the server and write one term
    [eclipse 32]: socket(unix, datagram, s), connect(s, '/tmp/sock').

    yes.
    [eclipse 33]: printf(s, "%w. %b", message(process1)).

process2:
    % Connect a named socket to the server and write another term
    [eclipse 15]: socket(unix, datagram, s), connect(s, '/tmp/sock'),
        bind(s, '/tmp/socka').

    yes.
    [eclipse 16]: printf(s, "%w. %b", message(process2)).

    yes.
    % And now disconnect the output socket from the server
    [eclipse 17]: connect(s, 0).

    yes.

server:
    % Now the server can read the two terms
    X = message(process1)
    Y = message(process2)
    yes.
    % and it writes one term to the second process on the same socket
    [eclipse 12]: connect(s, '/tmp/socka'),
        printf(s, "%w. %b", message(server)).

process2:
    % 
    [eclipse 18]: read(s, Msg).

    Msg = message(server)
    yes.


next up previous index
Next: Stream Connection Between Up: Interprocess Communication Previous: Stream Connection on



Micha Meier
Mon Mar 4 12:11:45 MET 1996