[erlang-questions] What kind of objects can be a message type?
Lhfcws GMail
lhfcws@REDACTED
Fri Nov 21 13:05:47 CET 2014
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 <nick@REDACTED> 写道:
> 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
>>> erlang-questions@REDACTED
>>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>
More information about the erlang-questions
mailing list