5.4 Datagram Sockets

Datagram sockets are for connectionless use. This implies that there is no distinction between server and client. The basic patterns of use are as follows.

5.4.1 Initialization

First we create and initialize two socket objects S and T with the type datagram:

S={New Open.socket init(type:datagram)}
T={New Open.socket init(type:datagram)}

Both sockets can be named in the Internet domain as follows:

SPN={S bind(port:$)}
TPN={T bind(port:$)}

5.4.2 Exchanging Data

To send the virtual string 'really '#"great" from socket S to socket T, we feed

{S send(vs:'really '#"great" port:TPN)}

Data can be received as follows:

{T receive(list:?Is port:?P)}
{Browse Is#' from: '#P}

and Is is bound to the string "really great" and P to the port number SPN. The roles of sender and receiver may be toggled as you like.

To send to a socket on a different computer, the hostname of the computer can be specified. For example,

{S send(vs:'donald' host:'duck.somewhere' port:4711)}

would send 'donald' to the socket with port number 4711 on the computer duck.somewhere. Similarly, by using the field host for the receive method the sending computer can be found out.

5.4.3 Peer

An additional feature of the datagram socket is the ability to declare a socket as peer . Suppose we want to declare socket S as a peer of socket T. This is done by

{S connect(port:TPN)}

Now any message sent from socket S is received by socket T. Furthermore, it suffices to specify just the data to be sent. Hence

{S send(vs:"really great")}

has the same effect as the previous example. As an additional effect, only data sent by socket T is received at socket S. After a peer has been assigned, you can even use the methods read and write.

Disconnecting is the same as for stream sockets.

Christian Schulte
Version 1.4.0 (20080702)