[ Stream I/O | Reference Manual | Alphabetic Index ]

new_socket_server(?Socket, ?Address, +Queue)

Opens a new IP socket server stream with a maximum of Queue connections.
Socket
Socket server name (atom or variable)
Address
Address for socket connection (Host/Port or variable)
Queue
Number of connections allowed (integer)

Description

Opens a new IP stream socket server Socket at host Host and port number Port. It is allowed a maximum of Queue connection requests. This predicate combines the calls to socket/3, bind/2, listen/2 as follows:

       new_socket_server(Soc, Address, N) :-
           socket(internet, stream, Soc), 
           bind(Soc, Address),
           listen(Soc, N).
   

After creation of the socket server, accept/2 can be called to accept client socket stream connections. Socket is closed if either the bind/2 or listen/2 calls throws an exception and thus a server socket cannot be made.

Modes and Determinism

Examples


% Set up a socket server and accept a socket connection. The following will
% print the Port number used for the server, and waits for a client connection
[eclipse 26]: new_socket_server(Server, localhost/Port, 1), writeln(Port), accept(Server, _, Socket).
27694
....

% On a different ECLiPSe process running on the same machine (as localhost
% is used for the host name)

socket(internet, stream, Client), connect(Client, localhost/27694).

Client = 9
Yes (0.00s cpu)
[eclipse 3]: 
...

% On the original ECLiPSe process, the accept/3 call will now return
[eclipse 26]: new_socket_server(Server, localhost/Port, 1), writeln(Port), accept(Server, _, Socket).
27694

Port = 27694
Server = 9
Socket = 11
Yes (0.00s cpu)
% The Server can now be closed while the socket stream remains connected
[eclipse 27]: close(9).

See Also

accept / 3, socket / 3, bind / 2, listen / 2