[英]“Bin” problems about receive {tcp,Socket,Bin} in communication between c# and erlang
當我從c#-Part發送數據:“ hello world”時,我收到了數據並將其放在test_out.txt中,在其中我發現了“ hello world”。如果沒有功能binary_to_term,應該是嗎? 然后我當然得到了錯誤:tcp_closed和客戶端有點亂碼。 ---->謝謝
[服務器]二郎插座代碼:
loop(Socket) ->
receive
{tcp,Socket,Bin} ->
io:format("server received binary = ~p~n",[Bin]),
file:write_file("test_out.txt", Bin),
B="welcome to unity3d:",
Response=[B | Bin],
io:format("server replying = ~p~n",[Response]),
gen_tcp:send(Socket,term_to_binary(Response)),
[客戶] C#-sockets碼
TcpClient client = new TcpClient("127.0.0.1", port);
byte[] data = System.Text.Encoding.UTF8.GetBytes(str);
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);
在將其寫入文件之前,不必使用binary_to_term
。 二進制是字節序列,因此將其寫入文件可以很好地工作。
當您想通過網絡發送復雜的Erlang術語時,使用binary_to_term
。 它添加了其他信息。
1> String = "Hello". #Five letter string
"Hello"
2> Bin = term_to_binary(String). #Encode it with term_to_binary
<<131,107,0,5,72,101,108,108,111>> #9 bytes! You get info, that it is a list and it is of length 5
3> io:format("~s~n", [Bin]). #print the binary as it would appear in file
k^@^EHello
ok
如果要發送要寫入文件的字符串,則可以只使用二進制文件,而不必將其轉換為列表或其他Erlang術語。
在Erlang中,套接字屬於創建它的進程。 如果您的過程結束-套接字已關閉。 您要么沒有遞歸調用loop
要么進程崩潰了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.