Sockets are endpoints for communication between processes or hosts.
The most common model of communication is the client-server one, in which the "server" process or host is running all the time, waiting for requests to serve, and the "client" process or host sends requests to the server and get the reply from it for further processing.
The typical live cycle of a server socket is create-bind-listen-accept-read-write-close, while that of the client is create-connect-write-read-close. The status%socket and message%socket variables will be set accordingly after each operation. However, in the following examples, the status and message variables are ignored for simplicity.
A Typical Server Process:
socketcreate serverSock;
socketbind serverSock address="the.server.com" port=100;
while (true) {
socketlisten serverSock;
socketaccept serverSock newsocket=msgSock;
socketread msgSock;
display "Request: {msgSock%read:content}<br>
";
if ('{msgSock%read:content}' == 'quit') break;
socketwrite msgSock content="I heard: {msgSock%read:content}";
socketclose msgSock;
}
socketclose serverSock;
A Typical Client Process:
socketcreate clientSock;
socketconnect clientSock address="the.server.com" port=100;
socketwrite clientSock content="hello world";
socketread clientSock;
display "Reply: {clientSock%read:content}<br>
";
socketclose clientSock;
|