[erlang-questions] : strange behaviour of gen_tcp:connect

Raimo Niskanen raimo+erlang-questions@REDACTED
Wed Apr 8 16:23:07 CEST 2009


On Wed, Apr 08, 2009 at 12:27:24PM +0100, Oscar Hellström wrote:
> Hi,
> 
> If the port really is free, then you will get a econnrefused, unless you
> have a firewall blocking connection attempts.
> 
> Not that I have a windows server 2003 machine, but I'm still seeing at
> least one interesting thing here:
> 
> First I start a listening socket on port 6666:
> ~% netcat -l -p 6666
> 
> Then I configure my iptables to trop all packets on the local interface
> to that port:
> # iptables -A INPUT -i lo -p tcp --dport 6666 -j DROP
> 
> Then, I start an erlang shell and try to connect to port 6666:
> ~% erl
> Erlang (BEAM) emulator version 5.6.5 [source] [64-bit] [smp:2]
> [async-threads:0] [hipe] [ker
> 
> Eshell V5.6.5  (abort with ^G)
> 1> gen_tcp:connect({127,0,0,1}, 6666, [binary, {packet, 0}]).
> {error,etimedout}
> 
> Ok, wait here now a second, etimedout? This is from the gen_tcp manual:
> connect(Address, Port, Options) -> {ok, Socket} | {error, Reason}
> connect(Address, Port, Options, Timeout) -> {ok, Socket} | {error, Reason}
> ...
> Timeout = int() | infinity
> ...
> The optional Timeout parameter specifies a timeout in milliseconds. The
> default value is infinity.
> 
> So, shouldn't we wait for infinity?
> 3> timer:tc(gen_tcp, connect, [{127,0,0,1}, 6666, [binary, {packet, 0}]]).
> {189001895,{error,etimedout}}
> 5> 189001895 / 1000000 / 60.
> 3.150031583333333
> 
> It seems the definition of infinity here is 3.15 minutes?

It is most probably the definition of fatal timeout in the
TCP stack. So the man page forget to mention that if
the OS connect() call returns with a timeout, gen_tcp:connect
will also return with a timeout, even if none was specified.

> 
> Even when I specify the timeout explicitly I get the same result.
> 6> timer:tc(gen_tcp, connect, [{127,0,0,1}, 6666, [binary, {packet, 0}],
> infinity]).
> {188997939,{error,etimedout}}
> 7> 188997939 / 1000000 / 60.
> 3.14996565
> 
> Connecting to a different port, which I haven't firewalled I get the
> following:
> 8> gen_tcp:connect({127,0,0,1}, 6667, [binary, {packet,
> 0}]).                      
> {error,econnrefused}
> 
> Removing the firewall I get:
> # iptables -D INPUT -i lo -p tcp --dport 6666 -j DROP
> 9> timer:tc(gen_tcp, connect, [{127,0,0,1}, 6666, [binary, {packet, 0}],
> infinity]).
> {527,{ok,#Port<0.434>}}
> 
> Why is infinity so short?
:

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list