Erlang gen_server eaddrnotavail在16358 gen_tcp之后:连接调用

时间:2015-09-18 06:40:17

标签: macos erlang connect gen-server

我在Erlang中编写服务器并希望打开大量连接。问题是我在16358 gen_tcp:connect/3次来电后收到 {error,eaddrnotavail} 。如下所示,服务器非常简单。

我修改了我的MacOS Yosemite限制; ulimit -n输出为6553600。

我从:

开始
erl +Q 134217727 +P 1000000 -env ERL_MAX_PORTS 40960000 -env ERTS_MAX_PORTS 40960000

服务器:

-module(naive_server).
-author("Stefan Stan").

-compile(export_all).

start_server(Port) ->   
  Pid = spawn_link(fun() ->
    {ok, Listen} = gen_tcp:listen(Port, [binary, {backlog, 6553600}]),
    spawn(fun() -> acceptor(Listen, 0) end),
    timer:sleep(infinity)
  end),
  {ok, Pid}.

acceptor(ListenSocket, Nr) -> 
  {ok, Socket} = gen_tcp:accept(ListenSocket),
  io:format("Client nr ~p connected~n", [Nr]),
  spawn(fun() -> acceptor(ListenSocket,Nr+1) end),
  handle(Socket).

handle(Socket) ->
  receive
    {tcp, Socket, <<"quit", _/binary>>} ->
      gen_tcp:close(Socket);
    {tcp, Socket, Msg} ->
      gen_tcp:send(Socket, Msg),
      handle(Socket)
  end.

客户:

-module(naive_client).
-author("Stefan Stan").

%% API
-export([connect_clients/3, connect/1]).

connect(Port) ->
  gen_tcp:connect({127,0,0,1}, Port, []).

connect_clients(Number, Port, List) when is_number(Number), Number >= 0, is_number(Port), Port>0 ->
  F =
    fun() ->
      case Number of
        0 -> {ok, lists:reverse(List)};
        _ ->
          {ok, Pid} = connect(Port),
          NewList = [Pid|List],
          connect_clients(Number-1, Port, NewList)
      end
    end,
  spawn(F).

1 个答案:

答案 0 :(得分:3)

我认为,您可能已经用完了短暂的传出端口(由您的客户端使用)。你能跑吗:

macosx$ sysctl net.inet.ip.portrange.first net.inet.ip.portrange.last

在我的机器上它给了我:

net.inet.ip.portrange.first: 49152
net.inet.ip.portrange.last: 65535

差异是16383,非常接近你的数字。