1

我正在尝试通过 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.
4

1 回答 1

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())
于 2013-08-13T03:04:46.150 回答