当我发送数据时:"你好世界"从c#-Part中,我收到了数据并将其放到了test_out.txt中,在其中我找到了#34; 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);
答案 0 :(得分:1)
在将其写入文件之前,您不必使用binary_to_term
。二进制是字节序列,因此将其写入文件非常合适。
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
,要么进程崩溃。