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

Karl Velicka karolis.velicka@REDACTED
Tue Feb 6 09:26:38 CET 2018


The case where you are in control of both the server and the client is not
so common outside of book examples, so you'd be only configuring your end
of the connection. And even when you control both, there can be many
reasons for configuring your sockets differently.

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

OK, so the same options need to be set for listen / accept? That seems kind
of redundant--is there a reason for that? Or are these usually different
options?

On Tue, Feb 6, 2018 at 12:14 AM, Karl Velicka <karolis.velicka@REDACTED>
wrote:

> 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/4a0e038e/attachment.htm>


More information about the erlang-questions mailing list