[erlang-questions] strange behaviour of gen_tcp:connect
Oscar Hellström
oscar@REDACTED
Wed Apr 8 13:27:24 CEST 2009
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?
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?
Gamoto wrote:
> I an windows server 2003. I don't understand why gen_tcp:connect returns ALWAYS an error.
> "connect" should be blocking, the port is free, I didn't precise a timeout. I also tried with "localhost".
> Another strange effect: error returned is "econnrefused" or, when another emacs is opened with erlang code inside, the returned error is "timeout" !!!
> Is it "normal" with Erlang ?
>
> John
>
> start()->
> case gen_tcp:connect("127.0.0.1",1688,[binary, {packet, 0}]) of
> {ok,Socket}->
> io:format("Socket established~n"),
> loop(Socket);
> {error,Reason}->
> io:format("Error on connect: ~s~n",[Reason])
> end.
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
Best regards
--
Oscar Hellström, oscar@REDACTED
Office: +44 20 7655 0337
Mobile: +44 798 45 44 773
Erlang Training and Consulting Ltd
http://www.erlang-consulting.com/
More information about the erlang-questions
mailing list