[erlang-questions] TCP Kernel bug/feature

Martin Carlson martin@REDACTED
Thu May 17 13:18:24 CEST 2007


Hi all,

Came across a strange Linux kernel bug/feature this morning while testing
a TCP application we've written.

The following code captures the behavior.

server(Ip, Port) ->
    {ok, Socket} = gen_tcp:listen(Port, [{ip, Ip}, {reuseaddr, true},
                                         {packet, 4}, binary]),
    {ok, Client} = gen_tcp:accept(Socket),
    gen_tcp:close(Socket),
    Client.


client(Ip, Port) ->
    case gen_tcp:connect(Ip, Port, [{packet, 4}, binary]) of
        {ok, Client} ->
            Client;
        {error, econnrefused} ->
            client(Ip, Port);
        {error, Reason} ->
            erlang:error(Reason)
    end.


node1() ->
    server({127,0,0,1}, 12345),
    halt().


node2() ->
    client("127.0.0.1", 12345),
    io:format("Connection lost~n"),
    client("127.0.0.1", 12345).

When running node1() on one Erlang node and node2 on another
this creates a socket connected to itself. One can capture the socket and
pass data forth and back.

netstat gives the following result:
tcp        0      0 127.0.0.1:12345         127.0.0.1:12345        
ESTABLISHED

This however only seem to work on Suse 9.3 with a stock kernel and has
probably been fixed in newer versions of the kernel.

uname say:
2.6.11.4-20a-smp #1 SMP Wed Mar 23 21:52:37 UTC 2005 i686 i686 i386 GNU/Linux


--
Martin Carlson
Erlang Training & Consulting
http://www.erlang-consulting.com




More information about the erlang-questions mailing list