[erlang-questions] Socket connects to itself

Per Hedeland per@REDACTED
Tue Oct 13 23:05:31 CEST 2009


Tony Rogvall <tony@REDACTED> wrote:
>
>I did some experiments (yes I am a bit bored right now).
>I tried some of the low levels calls for gen_tcp:connect to see what  
>is going on:
>
>On my mac:
>{ok,S} = inet:open(-1,{0,0,0,0},0,[{active,true}],tcp,inet,inet_tcp).
>{ok,{_,Port}} = inet:sockname(S).
>prim_inet:connect(S, {127,0,0,1}, Port, infinity).
>Result is {error,einval}

Interesting.

>Same thing on my linux (ubuntu 9.04) / vmware (on my mac :-)
>
>{ok,S} = inet:open(-1,{0,0,0,0},0,[{active,true}],tcp,inet,inet_tcp).
>{ok,{_,Port}} = inet:sockname(S).
>prim_inet:connect(S, {127,0,0,1}, Port, infinity).
>Result is {ok,#Port<..>}
>
>I like this one ;-) No one is listening .... how about you?

Yes, this is what I elaborated on in my first message in the thread. The
connect() sends a SYN, which loops back into the TCP input side, where
it finds the socket in SYN_SENT state (obviously). Per the spec, SYN
reception in SYN_SENT should cause transition to SYN_RCVD and the
connection should be established. It works on FreeBSD too, so it must be
The Right Thing(tm).:-)

1> {ok,S} = inet:open(-1,{0,0,0,0},0,[{active,true}],tcp,inet,inet_tcp).
{ok,#Port<0.423>}
2> {ok,{_,Port}} = inet:sockname(S).
{ok,{{0,0,0,0},59004}}
3> prim_inet:connect(S, {127,0,0,1}, Port, infinity).
ok
4> gen_tcp:send(S, <<"foo">>).
ok
5> flush().
Shell got {tcp,#Port<0.423>,"foo"}
ok

--Per


More information about the erlang-questions mailing list