博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Erlang脚印 0003] gen_tcp
阅读量:5907 次
发布时间:2019-06-19

本文共 6410 字,大约阅读时间需要 21 分钟。

常用的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>

 

转载于:https://www.cnblogs.com/hanbird/p/3837280.html

你可能感兴趣的文章
6.线程同步
查看>>
HTML5坦克大战
查看>>
20120122 linux 启动流程
查看>>
音视频同步原理解析
查看>>
mac下更新自带的PHP版本到5.6或7.0
查看>>
我的新博客开通了~
查看>>
lvs-heartbeat
查看>>
【标题】
查看>>
话说大数据:非互联网企业从小数据开始
查看>>
看了隋老师的挨踢人物传之后的一些想法
查看>>
未来架构师的平台战略范例(3)_DOCKER
查看>>
RAID 5数据恢复
查看>>
太航CRM软件
查看>>
RH436集群详细笔记
查看>>
LANTEK EXPERT 功能详解
查看>>
我的友情链接
查看>>
Server-u搭建FTP服务器
查看>>
freebsd nfs 挂载遇到的问题
查看>>
宏定义中的‘#’、‘##’操作符和‘...’、‘ __变参__ ’
查看>>
客户端(页面)查询与高亮查询结果插件 jquery-fastsearch(基于jQuery)
查看>>