[erlang-questions] TCP Kernel bug/feature

Martin Carlson <>
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),

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

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

node2() ->
    client("", 12345),
    io:format("Connection lost~n"),
    client("", 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        

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: #1 SMP Wed Mar 23 21:52:37 UTC 2005 i686 i686 i386 GNU/Linux

Martin Carlson
Erlang Training & Consulting

More information about the erlang-questions mailing list