常用的gen_tcp用到的函数:
gen_tcp:listen(Port, Options) -> {ok, ListenSocket}|{error, Reason} //监听端口
Options:
{active, true}:创建一个主动套接字;当数据到达时候,控制进程会发送{tcp, Socket, Data}消息, 无法控制客户端的发送量,有导致服务器来不及处理消息的风险。
{active, false}: 创建一个被动套接字;控制进程必须调用gen_tcp:recv(Socket, N)来接收来自于套接字的数据,可以控制客户端的发送量。
{active, once}: 创建一个主动套接字, 但是只接收一条消息,如果需要接收下一条消息,必须再激活它。
inet:setopts(Socket, Options) -> ok|{error, posix()} //设置socket的一些属性
inet:peername(Socket) -> {ok, {IP_Address, Port}}| {error, Why} //返回的是客户端的IP地址和端口号,{N1,N2,N3,N4} 代表IPV4,{K1,K2,K3,K4,K5,K6,K7,K8}代表IPV6。
gen_tcp:accept(ListenSocket) -> {ok, Socket}|{error,Reason} //接收来自客户端的socket链接
gen_tcp:connect(Address, Port, Options) -> {ok, Socket}|{error, Reason} // 连接socket服务器
gen_tcp:send(Socket, Packet) -> ok | {error, Reason} // 发送数据
gen_tcp:close(Socket) -> ok // 关闭Socket
gen_tcp:shutdown(Socket, How) -> ok|{error, Reason} // 关闭socket
构建一个简易的Tcp Socket通讯
1 -module(tcp_socket1). 2 -compile(export_all). 3 4 start_server() -> 5 start_server(6000). 6 7 start_server(Port) -> 8 {ok, ListenSocket} = gen_tcp:listen(Port, [binary,{packet, 0}, {active, true}]), 9 io:format("Server is running ....."), 10 par_connect(ListenSocket). 11 12 par_connect(Listen) -> 13 {ok, Socket} = gen_tcp:accept(Listen), 14 spawn(fun() -> par_connect(Listen) end), 15 loop(Socket). 16 17 loop(Socket) -> 18 receive 19 {tcp, Socket, Data} -> 20 io:format("Server receive binary:~p~n", [Data]), 21 RecStr = binary_to_term(Data), 22 io:format("Server receive binary_to_term:~p~n", [RecStr]), 23 gen_tcp:send(Socket,term_to_binary(RecStr)), 24 io:format("Server send to client binary:~p~n", [term_to_binary(RecStr)]), 25 loop(Socket); 26 {tcp_closed, Socket} -> 27 io:format("Client socket closed") 28 end. 29 30 start_client() -> 31 start_client("localhost", 6000). 32 33 start_client(Address, Port) -> 34 {ok, Socket} = gen_tcp:connect(Address, Port, [binary, {packet, 0}, {active, true}]), 35 ok = gen_tcp:send(Socket, term_to_binary("Nice to meeet you!")), 36 receive 37 {tcp, Socket, Data} -> 38 io:format("Client receive binary:~p~n", [Data]), 39 RecStr = binary_to_term(Data), 40 io:format("Cleint receive binary_to_term:~p~n", [RecStr]), 41 gen_tcp:close(Socket) 42 end. 43
打开两个客户端分别运行服务器和客户端。
服务器窗口显示的信息:
Eshell V5.10.4 (abort with ^G)1> c(tcp_socket1).{ok,tcp_socket1}2> tcp_socket1:start_server().Server is running .....Server receive binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109,101,101, 101,116,32,121,111,117,33>>Server receive binary_to_term:"Nice to meeet you!"Server send to client binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109, 101,101,101,116,32,121,111,117,33>>Client socket closedokServer receive binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109,101,101, 101,116,32,121,111,117,33>>Server receive binary_to_term:"Nice to meeet you!"Server send to client binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109, 101,101,101,116,32,121,111,117,33>>Client socket closedServer receive binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109,101,101, 101,116,32,121,111,117,33>>Server receive binary_to_term:"Nice to meeet you!"Server send to client binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109, 101,101,101,116,32,121,111,117,33>>Client socket closed3>
客户端窗口显示的信息:
4> tcp_socket1:start_client().Client receive binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109,101,101, 101,116,32,121,111,117,33>>Cleint receive binary_to_term:"Nice to meeet you!"ok5> tcp_socket1:start_client().Client receive binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109,101,101, 101,116,32,121,111,117,33>>Cleint receive binary_to_term:"Nice to meeet you!"ok6> tcp_socket1:start_client().Client receive binary:<<131,107,0,18,78,105,99,101,32,116,111,32,109,101,101, 101,116,32,121,111,117,33>>Cleint receive binary_to_term:"Nice to meeet you!"ok7>