Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4995176
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: Erlang

2015-03-02 13:32:13

由于Erlang在处理数据时在性能上具有一定的优越性,特别是在处理并发计算的时候。于是想着能不能实现C#与Erlang之间的通讯,经过一天的编码,终于有所收获。

首先要注意的是,在Erlang与C#之间,进行数据交换之前,都必须把数据转换成UTF8的格式后,再获取其二进制数据,同时获取时也需要以UTF8的格式获取,否则会出现乱码的现象,在这次实现的两者之间的Socket通讯,不仅可以传送字母,数字,还可以传送中文字符,这一切都需要感谢R13版本中新增了处理unicode字符的unicode模块。

下面是erlang的代码。

1、Socket的监听器,当数据到达了,把数据写入test_out.txt文件中,并且回复成功报文。

  1. -module(tcp_server).

  2. -compile([export_all]).

  3.  

  4. start_server() ->

  5. {ok,Listen} = gen_tcp:listen(2345,[binary,{packet,0},{reuseaddr,true},{active,true}]),

  6. seq_loop(Listen).

  7.  


  8. seq_loop(Listen) ->

  9. {ok,Socket} = gen_tcp:accept(Listen),

  10. loop(Socket),

  11. seq_loop(Listen).

  12.  


  13. loop(Socket) ->

  14. receive

  15. {tcp,Socket,Bin} ->

  16. io:format("server received binary = ~p~n",[Bin]),

  17. file:write_file("test_out.txt", Bin),


  18. %[DescList] = io_lib:format("~ts", ["OK"]),

  19. %DescBin = erlang:iolist_to_binary(DescList),

  20. %DescList2 = unicode:characters_to_list(DescBin),

  21. %List = DescList2,

  22. %Bin1 = unicode:characters_to_binary(List),

  23. Bin1 = unicode_test:test2(),

  24. io:format("server replying = ~p~n",[Bin1]),

  25. gen_tcp:send(Socket,Bin1),

  26. loop(Socket);

  27. {tcp_closed,Socket} ->

  28. io:format("server socket closed ~n")

  29. end.


2、下面一段代码是获取获取当回复的数据位中文时的二进制代码,上面那段代码中的回复中文字符转换成二进制数据时会出错,不知道什么原因,但是把相同的代码拷贝到另外一个模块中,然后再调用,就能正常的运行,很郁闷。不过上面的那段注释了的代码如果处理非中文字符却是正常的。


  1. -module(unicode_test).

  2. -compile([export_all]).

  3.  

  4. test2() ->

  5.     [DescList] = io_lib:format("~ts", ["处理成功"]),

  6.     DescBin = erlang:iolist_to_binary(DescList),

  7.     DescList2 = unicode:characters_to_list(DescBin),

  8.     Bin = unicode:characters_to_binary(DescList2),

  9.     Bin.

以上的都是erlang代码,下面的是C#代码:


  1. static void Main(string[] args)

  2.         {

  3.              Int32 port = 2345;

  4.  

  5.              string str = "在list中,每个unicode字符采用integer来表示,因此与latin1的list相比,unicode list中,element的数值可以大于255。下面就是一个有效的unicode list: [1024, 1025]";

  6.  

  7.              TcpClient client = new TcpClient("localhost", port);

  8.              byte[] data = System.Text.Encoding.UTF8.GetBytes(str);

  9.              NetworkStream stream = client.GetStream();

  10.              stream.Write(data, 0, data.Length);

  11.  

  12.              Console.WriteLine("Sent: {0}", str);

  13.  

  14.              data = new Byte[256];

  15.  

  16.              // String to store the response ASCII representation.

  17.              String responseData = String.Empty;

  18.  

  19.              // Read the first batch of the TcpServer response bytes.

  20.              Int32 bytes = stream.Read(data, 0, data.Length);

  21.              responseData = System.Text.Encoding.UTF8.GetString(data, 0, bytes);

  22.              Console.WriteLine("Received: {0}", responseData);

  23.  

  24.              Console.ReadKey(true);

  25.  

  26.         }

在erl shell中利用c("unicode_test")、c("tcp_server")代码编码上面的两个模块后,后执行tcp_server:start_server()。然后在C#执行代码,就能看到效果了。


原文链接



阅读(2683) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~