[erlang-questions] spawn_link

Thilani Abeysinghe <>
Mon Dec 29 12:16:28 CET 2008


Thanks for quick reply.:)
function connect_to_server method tries to connect to a server, when it
connects sucessfull it returns
{ok,Socket}.
so I want to get that socket and send messages to the server. So here I'm
trieng to save the socket value in
state of gen_server.
regards
Thilani


On Mon, Dec 29, 2008 at 4:40 PM, Chandru <
> wrote:

> 2008/12/29 Thilani Abeysinghe <>:
> > I have a problem of how to get the return value of a function when
> >
> > spawn_link(Fun) ->pid() is used.
> >
> > Im going to use above function in following manner.
> >
> > init([]) ->
> >
> > ChildPid = erlang:spawn_link(?MODULE, connect_to_server, [Ip,Port, ConId,
> > self(),State]).
> >
> > connect_to_server(Ip, Port, ConId, ParentPid,State) ->
> >   case Result= gen_tcp:connect(Ip, Port,
> >   [binary, {packet, 0}]) of
> >   {ok, Socket} ->
> >            io:format("Socket Received:~w~n",[Socket]),
> >           ok =(catch gen_server:call(g_srv, {add_conn, self(), ConId,
> > Socket})),
> >           NewState = State#state{socket= Socket};
> >   {error, Reason} ->
> >           io:format("connection error host Ip:~w Port: ~w ~w ~n",
> > [Ip,Port,Reason]) ,
> >          connect_to_server(Ip, Port, ConId, ParentPid,State)
> >   end.
> >
> >  I want to get the return value of connect_to_server function. How can I
> do
> > that.
> >
>
> I'm assuming you want to send intermediate results, as
> connect_to_server seems to be part of a process which is long lived.
> In such cases, send a message to the parent process from the spawned
> process.
>
> cheers
> Chandru
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081229/11dd2ff2/attachment.html>


More information about the erlang-questions mailing list