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根据文件:
是传递给connect函数的选项列表[binary,{packet,0}]
表示套接字上发送/接收的数据是二进制格式(与列表格式相反)binary
,有点混乱,文档中似乎没有介绍。在与Freenode上的#erlang中一些有知识的家伙交谈后,我发现{packet,0}
选项指定了多少字节表示数据包长度。在幕后,数据包的长度被剥离,erlang只向您发送数据包wit没有长度。因此,packet
与没有长度的原始数据包相同,除了数据的接收者之外,所有内容都会被处理。有关这方面的更多信息,请查看{packet,0}
希望对您有所帮助。{packet,0}用于指示TCP数据以未经修改的形式直接传递给应用程序 二进制意味着接收到的数据包是以二进制的形式发送的。(但您仍然可以使用gen_tcp:send和类似“message”的消息)