Short lived connections

The operating system limits the number of simultaneous connections open by a process at one time. The limit various with different operating systems but is typically 64 connections.

If the number is too small for your application, consider making connections persist only long enough for a send or receive. This way at most one connection would be alive at any time.

Here is an example of how you might implement sending and receiving in terms of short lived connections.

     send(To,Term):-    % +To, +Term
         my_address(MyAddress),
         tcp_connect(To, Socket),
         tcp_send(Socket, MyAddress-Term),
         tcp_shutdown(Socket).
     
     receive(From,Term):-  % -From, -Term
         repeat,
         tcp_select(term(Socket, From-Term)),
         !,
         tcp_shutdown(Socket).
     

In the above example, it is assumed that all processes are servers, and have asserted the address obtained from a call to tcp_create_listener/2 into my_address/1.

The performance penalty of short lived connections is the time for making and breaking connections, which is actually quite fast. For comparison, the time it takes to create and then destroy a connection to a server is hundreds of times slower than sending a character to the server, but is comparable to the time it takes to send a large term to the server.