[erlang-questions] Why is setting socket options by setopts()better than by listen()?
Valentin Micic
v@REDACTED
Thu Nov 27 08:26:27 CET 2008
I wouldn't say better... I think it is more a question of semantic: one is
used to set/change default parameters, whilst another to inform driver about
specific connection behavior. Relying on a (changed) default behavior may
not be always desirable, as it may make program less readable -
gen_tcp:listen/2 may be placed anywhere in a code, whereas the person that
reads the code may want to know about the socket behavior right there, where
the connection has been accepted.
I would say that options related to gen_tcp:listen/2, therefore not
influencing session socket directly, should be placed alongside listen, say:
gen_tcp:listen(Port, [{reuseaddr, true}])
OTOH, options related to the socket should be explicitly mentioned after the
connection has been accepted:
inet:setopts(Socket, [binary, {active, once}])
IMO, one exception to this rule should be "active" flag, which I also use as
a part of gen_tcp:listen/2 argument:
gen_tcp:listen(Port, [{reuseaddr, true}, {active, false}])
in order to prevent socket receiving any traffic without solicitation. But I
think this doesn't break the rule, as the call to inet:setopts/2 may change
that the way particular process requires it.
V.
-----Original Message-----
From: erlang-questions-bounces@REDACTED
[mailto:erlang-questions-bounces@REDACTED] On Behalf Of Sergey A.
Sent: 26 November 2008 10:45 PM
To: erlang-questions@REDACTED
Subject: [erlang-questions] Why is setting socket options by setopts()better
than by listen()?
Hello.
I've read the following in the "Programming Erlang":
-------------------8<-------------------
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)
-------------------8<-------------------
And I have a small question. Why that is considered better solution
than using the same options, but passed to gen_tcp:listen/2 function:
gen_tcp:listen(2345, [{packet, 4}, binary, {nodelay, true}, {active,
true}, {reuseaddr, true}])
As I know, all the sockets obtained by calling gen_tcp:accept/1
inherit options listed in gen_tcp:listen.
Thanks.
--
Sergey.
_______________________________________________
erlang-questions mailing list
erlang-questions@REDACTED
http://www.erlang.org/mailman/listinfo/erlang-questions
More information about the erlang-questions
mailing list