forked from jixiuf/helloerlang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_demo.erl
34 lines (29 loc) · 1.19 KB
/
tcp_demo.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
-module(tcp_demo).
-compile(export_all).
start_server(Port) ->
Pid = spawn_link(fun() ->
{ok, Listen} = gen_tcp:listen(Port, [binary, {active, false}]),
spawn(fun() -> acceptor(Listen) end),
timer:sleep(infinity) %sleep ,避免前当进程退出 ,因为tcp socket 是与启动它的进程绑定的,如果进程死,socket关。
end),
{ok, Pid}.
acceptor(ListenSocket) ->
{ok, Socket} = gen_tcp:accept(ListenSocket), %
spawn(fun() -> acceptor(ListenSocket) end), %每次一个客户端连接上来,启用另一个进程继续兼听,而当前进程则用来处理刚连接进来的client
handle(Socket).
%% Echoing back whatever was obtained
%% 与client 进行通信 ,这里仅仅是echo "erlang+msgfromClient"
handle(Socket) ->
inet:setopts(Socket, [{active, once}]),
receive
{tcp, Socket, <<"quit", _/binary>>} ->
gen_tcp:close(Socket);
{tcp, Socket, Msg} ->
gen_tcp:send(Socket, "erlang "++Msg),
handle(Socket)
end.
%%tcp_demo:start_server(8888).
%% telnet 127.0.0.1 8888
%%hello
%% will got
%% erlang hello