tcp服务器只接受一些连接

时间:2016-04-14 05:38:49

标签: sockets tcp erlang gen-tcp

我有一个tcp服务器和一个客户端。客户端会尝试连接到tcp服务器,每个进程生成1000个进程。我可以看到tcp服务器正在接受245个连接并拒绝所有其余的连接。每次,服务器都接受第一个245.

修正: 试图增加积压属性,但没有奏效 试图在每个进程生成后调用timer:sleep(N),以便我可以降低tcp服务器可以接受客户端的速率。失败!

服务器和客户端:

 accept(LSocket) ->
   case gen_tcp:accept(LSocket) of
     {ok, Socket} ->
       io:format("Accepted ~n"),
       {sockets, List} = hd(ets:lookup(csockets, sockets)),
       NewList = [Socket | List],
       ets:insert(csockets, {sockets, NewList}),
       Pid = spawn(fun() ->
        loop(Socket, 0)
              end),
       gen_tcp:controlling_process(Socket, Pid),
       accept(LSocket);
     {error, closed} ->
       error
    end.

客户端:

    send(State = #state{low = Low, high = Low}) ->
      NewState = receive_sockets(0, Low, State),
      NewState;
      send(State = #state{low = Low}) ->
      N = Low rem 2,
      Dest = lists:nth(2, State#state.dest),
      loop(self(), Dest, Low),
      NewState = State#state{low = Low + 1},
      send(NewState).

   loop(From, {IP, Port}, Low) ->
     case gen_tcp:connect(IP, Port, []) of
      {ok, Socket} ->
        gen_tcp:send(Socket, integer_to_binary(Low)),
        From ! {Low, Socket},
        Pid = spawn(?MODULE, receive_from_other_users, []),
        gen_tcp:controlling_process(Socket, Pid);
      _Else ->
        io:format("The connection failed ~n"),
        loop(From, {IP, Port}, Low)
   end.

0 个答案:

没有答案