Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sockets Erlang gen_tcp连接问题_Sockets_Tcp_Erlang - Fatal编程技术网

Sockets Erlang gen_tcp连接问题

Sockets Erlang gen_tcp连接问题,sockets,tcp,erlang,Sockets,Tcp,Erlang,简单的问题 此代码 client() -> SomeHostInNet = "localhost" % to make it runnable on one machine {ok, Sock} = gen_tcp:connect(SomeHostInNet, 5678, [binary, {packet, 0}]), ok = gen_tcp:send(Sock, "Some Data"),

简单的问题

此代码

client() ->
    SomeHostInNet = "localhost" % to make it runnable on one machine
    {ok, Sock} = gen_tcp:connect(SomeHostInNet, 5678, 
                                 [binary, {packet, 0}]),
    ok = gen_tcp:send(Sock, "Some Data"),
    ok = gen_tcp:close(Sock).
非常清楚,除了我不太明白[binary,{packet,0}]是什么意思? 有人愿意解释吗

MadSeb

根据文件:

  • [binary,{packet,0}]
    是传递给connect函数的选项列表
  • binary
    表示套接字上发送/接收的数据是二进制格式(与列表格式相反)
  • {packet,0}
    ,有点混乱,文档中似乎没有介绍。在与Freenode上的#erlang中一些有知识的家伙交谈后,我发现
    packet
    选项指定了多少字节表示数据包长度。在幕后,数据包的长度被剥离,erlang只向您发送数据包wit没有长度。因此,
    {packet,0}
    与没有长度的原始数据包相同,除了数据的接收者之外,所有内容都会被处理。有关这方面的更多信息,请查看

希望对您有所帮助。

{packet,0}用于指示TCP数据以未经修改的形式直接传递给应用程序

二进制意味着接收到的数据包是以二进制的形式发送的。(但您仍然可以使用gen_tcp:send和类似“message”的消息)