[erlang-questions] Setting socket options on listen / accept

Karl Velicka karolis.velicka@REDACTED
Tue Feb 6 09:14:20 CET 2018


Hi,

You don't have to set the options separately for accept, there's a two
argument version that takes a proplist of socket options.

When you're using gen_tcp:listen then you're setting up a server that
receives connections, whereas `accept` is connecting to something that's
listening to connections. You need to set the socket options separately in
each case.

Hope that helps!

On 6 Feb 2018 07:55, "Tianxiang Xiong" <tianxiang.xiong@REDACTED> wrote:

In Joe's Programming Erlang 2nd E
<https://pragprog.com/book/jaerlang2/programming-erlang>, Chpt. 17, pg.
274, he says:

After we have accepted a connection, it’s a good idea to explicitly set the
> required socket options, like this:


{ok, Socket} = gen_tcp:accept(Listen),
inet:setopts(Socket, [{packet,4},binary,{nodelay,true},{active, true}]),
loop(Socket)

Until then the book's been doing:

{ok, Listen} = gen_tcp:listen(2345, [binary, {packet, 4}, {reuseaddr,
true}, {active, true}])
{ok, Socket} = gen_tcp:accept(Listen),
...
loop(Socket)

Is there a reason we need to set the options after accept? Is it necessary
to set options on listen *and* accept?


_______________________________________________
erlang-questions mailing list
erlang-questions@REDACTED
http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180206/3a1b5ae5/attachment.htm>


More information about the erlang-questions mailing list