tcp_send(
+Socket,
+Term)
This sends Term to the process whose socket identifier is
Socket. Socket gets the term
term(From,Term) from tcp_select/[1,2]
(see ipc-tcp-trm-select1).
Note that tcp_send/2
can only be used to send terms to a Prolog server
as Term is sent in an encoded form that is efficiently decoded
by tcp_select/[1,2]
(see ipc-tcp-trm-select1).
Here is an example of how one might use tcp_send/2
to implement a
remote procedure call to some process whose file descriptor is P.
It is assumed that the connections between the two processes have been
established elsewhere.
:-use_module(library(basics), [member/2]). :-use_module(library(freevars), [free_variables/4]). % machine a has p_call/2 p_call(P, Goal):- free_variables(Goal, [], [], FreeVars), tcp_send(P, satisfy(FreeVars, Goal)), tcp_select(term(P, Bag)), member(FreeVars, Bag). % machine b has slave/0 slave:- T = term(P, satisfy(FreeVars, Goal)), repeat, tcp_select(T), findall(FreeVars, Goal, Bag), tcp_send(P, Bag), fail.
The use of library(freevars)
is to limit the amount of data being sent
by the slave to just those variables that may be instantiated by
calling Goal
.
For many applications this is all that is required. It has the
advantage of limiting both the frequency of messages sent
(findall/3
), and the size of the messages (free_variables/4
).
A better implementation of remote procedure call would allow the
caller to respond to solutions from several different machines as soon
as the solutions are generated, without waiting for the solutions to
be assembled into a list. This is attempted in the example program
IPC/TCP/demo/sibling.pl
(see ipc-tcp-exa).
If you try to send to a broken socket, the "Broken pipe" exception is raised:
existence_error(_,_,_,_,errno(32))