[erlang-questions] What kind of objects can be a message type?

T Ty <>
Fri Nov 21 16:25:46 CET 2014


You can pass Pid and References in a message using !

This is an example of how trivial writing a FTP-like client in Erlang.

On machine 1.
File = file:open(FileName, [read]).
PidOfProcessOnMachine2 ! {remote_file, File}.

On machine 2.
receive
    {remote_file, File} ->
       {ok, Data} = file:read(File, 1000000),
       write_to_local_disk(Data)
end

Now, that said I have only tried it with File /Pid references and not with
Socket.

On Fri, Nov 21, 2014 at 12:05 PM, Lhfcws GMail <> wrote:

> Thanks a lot!  gen_tcp:controlling_process is indeed what I want.
> And let me confirm your answer:
> 1.  Objects like socket and file handler can be pass in spawn arguments.
> 2.  But they can not be passed by ! message passing.
>
> Am I right?
> --------------
>
> 在 2014年11月21日,19:50,Niclas Eklund <> 写道:
>
> > Hello again!
> >
> > Forgot to answer your second question. Yes, you can spawn a new process
> that will "own" the connection, but then you need to use this function -
> http://www.erlang.org/doc/man/gen_tcp.html#controlling_process-2 You
> typically want  to keep the listen process rather lightweight (do very
> little) since using blocking accept is common and then the listen process
> needs to be restarted when doing a live code upgrade. See for example this
> code -
> https://github.com/erlang/otp/blob/maint/lib/orber/src/orber_iiop_net_accept.erl
> It's doing a bit more, i.e. only allows a certain number of concurrent
> incoming requests, but as you can see it's minimal.
> >
> > /Nick
> >
> > On 11/21/2014 12:43 PM, Niclas Eklund wrote:
> >> Hi!
> >>
> >> I'm afraid that you don't handle the returned values correctly. See
> this page - http://www.erlang.org/doc/man/gen_tcp.html - for more info.
> Should be:
> >>
> >> server() ->
> >>    {ok, LSock} = gen_tcp:listen(5678, [binary, {packet, 0},
> >>                                        {active, false}]),
> >>    {ok, Sock} = gen_tcp:accept(LSock),
> >>    {ok, Bin} = do_recv(Sock, []),
> >>    ok = gen_tcp:close(Sock),
> >>    Bin.
> >>
> >> do_recv(Sock, Bs) ->
> >>    case gen_tcp:recv(Sock, 0) of
> >>        {ok, B} ->
> >>            do_recv(Sock, [Bs, B]);
> >>        {error, closed} ->
> >>            {ok, list_to_binary(Bs)}
> >>    end.
> >>
> >> /Nick
> >>
> >>
> >>
> >> On 11/21/2014 08:44 AM, Lhfcws GMail wrote:
> >>> Hi, I’m new to Erlang.
> >>> I have a question about the message passed among the Erlang processes.
> >>> 1. I create a Socket or FileHandler, can I use ! to send it to another
> process and still works well?
> >>>
> >>> demo code:
> >>>
> >>>    ListenSocket = gen_tcp:listen(1055, [{active, true}]),
> >>>    Socket = gen_tcp:accept(ListenSocket),
> >>>    From ! {new_socket, Socket}.
> >>>
> >>>
> >>> 2. What if I invoking ’spawn’ to pass the Socket to the new process ?
> >>>
> >>> demo code:
> >>>
> >>>    NewProcess = fun(Socket) -> … end.
> >>>
> >>>    ListenSocket = gen_tcp:listen(1055, [{active, true}]),
> >>>    Socket = gen_tcp:accept(ListenSocket),
> >>>    spawn(NewProcess, Socket).
> >>>
> >>> =======
> >>>
> >>> I know that the message in the receiver is a copy of the one in the
> sender, but I’m a little confused about the questions above. Could someone
> just explain this?
> >>> Thank you.
> >>> _______________________________________________
> >>> erlang-questions mailing list
> >>> 
> >>> http://erlang.org/mailman/listinfo/erlang-questions
> >>
> >
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141121/483482ca/attachment.html>


More information about the erlang-questions mailing list