我正在尝试通过TCP套接字接收一些数据。当我运行下面的代码时,我得到输出:localhost ^ V ^ A ^ @,我知道你需要转换数据,如果使用二进制发送它,但因为我发送一个列表我认为它会收到相同的?为什么主机字符串显示正确但其他数据不显示?
非常感谢任何帮助,谢谢。
cell_process(Port, X, Y)->
Host = "localhost",
Data = [Host,Port,X,Y],
{ok, Socket} = gen_tcp:connect(Host, 22,
[list, {packet, 0}]),
ok = gen_tcp:send(Socket, Data),
ok = gen_tcp:close(Socket).
server_process(ClientList)->
{ok, Listening_socket} = gen_tcp:listen(22, [list, {packet, 0},
{active, false}]),
{ok, Socket} = gen_tcp:accept(Listening_socket),
case gen_tcp:recv(Socket,0) of
{ok,Message}->
io:fwrite(Message);
{error,Why}->io:fwrite(Why)
end.
答案 0 :(得分:2)
Data = [Host,Port,X,Y]
是一个iolist,而不是列表
gen_tcp:send会将数据转换为[<<"localhost">>,<<22:8>>]
这是iolist的文档:
iodata() = iolist() | binary()
iolist() maybe_improper_list(char() | binary() | iolist(), binary() | [])
maybe_improper_list() maybe_improper_list(any(), any())
byte() 0..255
char() 0..16#10ffffmaybe_improper_list(T) maybe_improper_list(T, any())