由于Erlang在处理数据时在性能上具有一定的优越性,特别是在处理并发计算的时候。于是想着能不能实现C#与Erlang之间的通讯,经过一天的编码,终于有所收获。
首先要注意的是,在Erlang与C#之间,进行数据交换之前,都必须把数据转换成UTF8的格式后,再获取其二进制数据,同时获取时也需要以UTF8的格式获取,否则会出现乱码的现象,在这次实现的两者之间的Socket通讯,不仅可以传送字母,数字,还可以传送中文字符,这一切都需要感谢R13版本中新增了处理unicode字符的unicode模块。
下面是erlang的代码。
1、Socket的监听器,当数据到达了,把数据写入test_out.txt文件中,并且回复成功报文。
- -module(tcp_server).
- -compile([export_all]).
-
- start_server() ->
- {ok,Listen} = gen_tcp:listen(2345,[binary,{packet,0},{reuseaddr,true},{active,true}]),
- seq_loop(Listen).
-
- seq_loop(Listen) ->
- {ok,Socket} = gen_tcp:accept(Listen),
- loop(Socket),
- seq_loop(Listen).
-
- loop(Socket) ->
- receive
- {tcp,Socket,Bin} ->
- io:format("server received binary = ~p~n",[Bin]),
- file:write_file("test_out.txt", Bin),
- %[DescList] = io_lib:format("~ts", ["OK"]),
- %DescBin = erlang:iolist_to_binary(DescList),
- %DescList2 = unicode:characters_to_list(DescBin),
- %List = DescList2,
- %Bin1 = unicode:characters_to_binary(List),
- Bin1 = unicode_test:test2(),
- io:format("server replying = ~p~n",[Bin1]),
- gen_tcp:send(Socket,Bin1),
- loop(Socket);
- {tcp_closed,Socket} ->
- io:format("server socket closed ~n")
- end.
- -module(unicode_test).
- -compile([export_all]).
-
- test2() ->
- [DescList] = io_lib:format("~ts", ["处理成功"]),
- DescBin = erlang:iolist_to_binary(DescList),
- DescList2 = unicode:characters_to_list(DescBin),
- Bin = unicode:characters_to_binary(DescList2),
- Bin.
以上的都是erlang代码,下面的是C#代码:
- static void Main(string[] args)
- {
- Int32 port = 2345;
-
- string str = "在list中,每个unicode字符采用integer来表示,因此与latin1的list相比,unicode list中,element的数值可以大于255。下面就是一个有效的unicode list: [1024, 1025]";
-
- TcpClient client = new TcpClient("localhost", port);
- byte[] data = System.Text.Encoding.UTF8.GetBytes(str);
- NetworkStream stream = client.GetStream();
- stream.Write(data, 0, data.Length);
-
- Console.WriteLine("Sent: {0}", str);
-
- data = new Byte[256];
-
- // String to store the response ASCII representation.
- String responseData = String.Empty;
-
- // Read the first batch of the TcpServer response bytes.
- Int32 bytes = stream.Read(data, 0, data.Length);
- responseData = System.Text.Encoding.UTF8.GetString(data, 0, bytes);
- Console.WriteLine("Received: {0}", responseData);
-
- Console.ReadKey(true);
-
- }
在erl shell中利用c("unicode_test")、c("tcp_server")代码编码上面的两个模块后,后执行tcp_server:start_server()。然后在C#执行代码,就能看到效果了。
原文链接