eaddrinuse

Dietmar Schaefer dietmar@REDACTED
Thu Apr 28 06:42:48 CEST 2005


Hi !


If I understand it right
 
 {reuseaddr, Boolean} as parameter for  gen_tcp:listen  allows local 
reuse of port numbers.


So the error eaddrinuse should not occur ?


Every time I try to reconnect to the server I get this error.


Here is my code:

 getMessages(Port) when integer(Port) ->
    ListenPid = spawn_link(fun() -> listenServer(Port) end).

getMessages() ->
    io:format("getMessages ~n"),
    getMessages(?LISTENPORT).


listenServer(Port) ->
    Opts = [{packet, raw}, {active, false},{reuseaddr, true}],
    case gen_tcp:listen(Port,Opts) of
        {ok,ListenSocket}  -> listenLoop(ListenSocket);
        {error,eaddrinuse} -> io:format("error eaddrinuse in 
listenServer ~n"),                             
        ok
    end.

listenLoop(ListenSocket) ->
    {ok,Sock} = gen_tcp:accept(ListenSocket),
    spawn_link(fun() -> listenHandler(Sock) end),   
    listenLoop(ListenSocket).


listenHandler(Sock) ->
    do_recv(Sock),
    io:format("do_recv terminated~n"),
    io:format("calling getMessages again ~n"),
    gen_tcp:close(Sock),
    getMessages().
   


Any hints


Regards


Dietmar



More information about the erlang-questions mailing list