1、实验环境:
联想小型机:
操作系统:RedHat Enterprise LinuxServer release6.4(Santiago)
内核版本号:Linux server1 2.6.32-358.el6.x86_64#1 SMP
CPU型号:Intel(R)Xeon(R)CPU
E7-4820 @2.00Ghz。
内存:132G
磁盘:289G
、基于erlang开发设计了TCPserver,
)tcp连接过程序的建立,server端的port数=clientport数+1;(2)port的打开的数量上限决定着tcp连接的个数。
,通过ulimit
–n 1000000。该值不能超过硬限制的个数。
假设要进一步提高。须要从软限制和硬限制双方面考虑,详细可參考:还有一方面/proc/sys/net/ipv4/ip_local_port_range的值决定了一个侦听端口所能接收的最大tcp连接数,改动该值能够通过vi
/etc/sysctl.conf,改动后运行sysctcl -p使其生效。提高连接数的一个方法是通过开多个侦听端口来接收client的连接。怎样改动操作系统所能打开的最大文件数和ip_local_port_range成为影响tcp连接总数的两个关键点。
Erlang虚拟机默认的port上限为65536, erlang17通过erl
+Q 1000000能够改动端口上限为1000000,利用erlang:system_info(port_limit)进行查询,系统能够打开的最大文件描写叙述符能够通过erlang:system_info(check_io)中的max_fds进行查看,查看系统当前port数量能够用erlang:length(erlang:ports())得到
、代码:
tcp_client:
-module(tcp_client).
]).
loop_start(Port,Num)->
case start(Port)of
system_limit
-> io:format("");
)
end.
start(Port)->
try
case gen_tcp:connect("127.0.0.1",Port,
[binary,{packet,raw},{active,true},{reuseaddr,true}])of
{ok,Socket}->Socket;
{error,Reason}-> {error,Reason}
end
catch
throw:T->T;
exit:R->R;
error:E->E
end.
send_data(Socket,Data)when
is_list(Data)orelse is_binary(Data)->
gen_tcp:send(Socket,Data),
receive
{tcp,Socket,Bin}->
io:format("recv~p~n",[Bin]);
{tcp_closed,Socket}->
io:format("receive server don't accept connection!~n")
end.
close(Socket)when is_port(Socket)->
gen_tcp:close(Socket).
generic_server:
-module(generic_server).
-behaviour(gen_server).
-define(TCP_OPTIONS,[binary,{packet,raw},{reuseaddr,true},{active,false}]).
]).
-export([]).
,maxconn}).
start(ServerName,Port,Max,{M,F})->
gen_server:start_link({local,ServerName},?MODULE,[#server_state{port=Port,maxconn=Max,loop={M,F}}],[]).
init([State=#server_state{port=Port}])->
case gen_tcp:listen(Port,?
TCP_OPTIONS)of
{ok,LSocket}->
{ok,accept(State#server_state{lsocket=LSocket})};
{error,Reason}->
{stop,{create_listen_socket,Reason}}
end.
accept(State=#server_state{lsocket=LSocket,loop=Loop,conn=Conn,maxconn=Max})->
Pid = spawn(generic_server,accept_loop,[self(),LSocket,Loop,Conn,Max]),
State.
accept_loop(Server,LSocket,{M,F},Conn,Max)->
erlang:group_leader(erlang:whereis(user),self()),
{ok,Sock} = gen_tcp:accept(LSocket),
if
true ->
gen_server:cast(Server,{accept_new,self()}),
M:F(Sock)
end.
handle_cast({accept_new,FromPid},State)->
Conn =State#server_state.conn,
]),
LSocket =State#server_state.lsocket,
Loop=State#server_state.loop,
Max=State#server_state.maxconn,
,Max]),
{noreply,
State}};
handle_cast({connect_close,FromPid},State)->
Conn=State#server_state.conn,
]),
}}.
echo_server:
-module(echo_server).
]).
start(Port)->
,
generic_server:start(echo_server,
Port, Max, {?
MODULE,loop}).
loop(Sock)->
)of
{ok,Data}->
gen_tcp:send(Sock,Data),
loop(Sock);
{error,closed}
->
io:format("client sock close~n"),
gen_server:cast(echo_server,{connect_close,self()})
end.
、通信流程
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemNjXzAwMTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center">
5、结论
设定ulimit –n 100000。假定ip_local_port_range 1 65535,侦听一个port的连接数为65534,再打开一个侦听port为100000-65534=34466,
单端口受制于ip_local_port_range的范围。总共的连接数为打开的文件描写叙述符个数。