Erlang入门——将字符串去““转为普通类型

"本文介绍了作者在学习Erlang过程中遇到的挑战,特别是关于分布式编程中使用gen_tcp进行跨节点通信的问题。作者详细阐述了如何处理接收到的二进制数据,并通过erl_scan、erl_parse和erl_eval模块将其转换为可用的Erlang值。在处理客户端发送的{4,5}
摘要由CSDN通过智能技术生成

前言:要不是种种原因,搞java的我为何会转这门小语言,真的难受。Erlang基础语法不难上手,函数式编程的思维和以前所学的不太一样。难在于,社区太小了,遇到不会的找不到他人经验,找人问都没地方问…

作为学习java和c过来的人,对社区经验分享与学习深有体会,反正只要我能留下来继续搞Erlang,自己碰到的问题以及琢磨出的解决办法都会分享。

步入正题。

(服务端用gen_tcp:listen/2打开端口生成Socket,gen_tcp:accept/2响应来自客户端gen_tcp:connect/2的连接,这个书上或官方文档有就不提了)

在分布式编程场景中,通过gen_tcp跨节点传值,设置了端口参数,类型为binary(二进制数据),{pakcet, 4}(收到的数据去掉头部)等等

...
-define(TCP_OPTIONS, [binary, {packet, 4}, {active, true}, {reuseaddr, true}]).

start(Port) ->
	{ok, LSocket} = gen_tcp:listen(Port, ?TCP_OPTIONS),
...

服务端接收到的数据用binary_to_term转换后是字符串形式(如果是可打印字符shell会转换成字符串,否则就是ASCII码的列表形式)。在Erlang里,字符串就是装着ASCII码的列表。然而字符串形式的数值用不了(至少我现在刚学不知怎么用),这时就要转换或提取字符串了。

核心方法:
erl_scan:string/1
erl_parse:parse_exprs/1
erl_eval:exprs/2

receive
		{tcp, Socket, Bin} ->
			Str = binary_to_term(Bin),
			Str1 = lists:subtract(Str, "\n"),
			
			%%字符串去""转为列表
			{ok, Scan1, _} = erl_scan:string(Str1 ++ "."),
			{ok, Scan2} = erl_parse:parse_exprs(Scan1),
			{_, Value, _} = erl_eval:exprs(Scan2, []),
			{X, Y} = Value,
			
			io:format("Server get the X: ~p ~n", [X]),
			io:format("Server get the Y: ~p ~n", [Y]),
			...

假设客户端输入 {4, 5} 传值过来
如果在每个操作之间进行打印

{tcp, Socket, Bin} ->
			io:format("Point C ~n"),
			Str2 = bitstring_to_list(Bin),
			io:format("Server get msg: ~p ~n", [Str2]),
			io:format("What is the type of the msg : ~p ~n", [is_atom(Str2)]),
			io:format("What is the type of the msg : ~p ~n", [is_list(Str2)]),
			io:format("What is the type of the msg : ~p ~n", [is_tuple(Str2)]),
			io:format("What is the type of the msg : ~p ~n", [is_bitstring(Str2)]),
			io:format("What is the type of the msg : ~p ~n", [is_reference(Str2)]),
			Str = binary_to_term(Bin),
			io:format("Server get msg: ~p ~n", [Str]),
			io:format("What is the type of the msg : ~p ~n", [is_atom(Str)]),
			io:format("What is the type of the msg : ~p ~n", [is_list(Str)]),
			io:format("What is the type of the msg : ~p ~n", [is_tuple(Str)]),
			io:format("What is the type of the msg : ~p ~n", [is_bitstring(Str)]),
			io:format("What is the type of the msg : ~p ~n", [is_reference(Str)]),
			Str1 = lists:subtract(Str, "\n"),
			io:format("Server get msg: ~p ~n", [Str1]),
			%%{X, Y} = io:parse_erl_form(Str1),
			%%{X, Y} = io:scan_erl_form(Str1),
			
			%%字符串去""转为列表
			{ok, Scan1, _} = erl_scan:string(Str1 ++ "."),
			{ok, Scan2} = erl_parse:parse_exprs(Scan1),
			{_, Value, _} = erl_eval:exprs(Scan2, []),
			io:format("Server get the Value: ~p ~n", [Value]),
			%%{X, Y} = list_to_tuple(Value),
			{X, Y} = Value,
			
			io:format("Server get the X: ~p ~n", [X]),
			io:format("Server get the Y: ~p ~n", [Y]),

输出效果是:
在这里插入图片描述
可见去掉""后,内部是什么类型的数据就会转为相应类型,如例子里是{4,5},为tuple类型。

参考自:
https://blog.csdn.net/weixin_30653097/article/details/94898270?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_title~default-0-94898270-blog-89374488.pc_relevant_show_downloadRating&spm=1001.2101.3001.4242.1&utm_relevant_index=3

https://www.cnblogs.com/daofen/p/6047789.html

小白入门,边学边分享。如有错误,敬请指正~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值