Erlang gen_tcp连接问题 [英] Erlang gen_tcp connect question

查看:369
本文介绍了Erlang gen_tcp连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单问题...

此代码..

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}]表示
任何人都要解释?

is very clear except that I don't quite understand what [binary, {packet,0}] means ? Any one cares to explain ?

MadSeb

推荐答案

根据 gen_tcp:connect 文档:


  • [binary,{packet,0}] 是传递给连接的选项列表函数。

  • binary 表示在套接字上发送/接收的数据是二进制格式(相反,列表格式。

  • {packet,0} ,有点混乱,文档中似乎没有涵盖。在与Freenode的#erlang中的一些知识渊博的聊天中,我发现数据包选项指定了多少个字节表示数据包长度。在幕后,长度从数据包和erlang只是发送数据包没有长度,因此 {packet,0} 与原始数据包相同,没有长度和每一个g被处理,但数据的接收者。有关详细信息,请查看 inet :setopts

  • [binary, {packet, 0}] is the list of options that's passed to the connect function.
  • binary means that the data that is sent/received on the socket is in binary format (as opposed to, say, list format.
  • {packet, 0}, is a little confusing and it doesn't appear to be covered in the documentation. After talking to some knowledgeable chaps in #erlang on Freenode, I found that the packet option specifies how many bytes indicate the packet length. Behind the scenes, the length is stripped from the packet and erlang just sends you the packet without the length. Therefore {packet, 0} is the same as a raw packet without a length and everything is handled but the receiver of the data. For more information on this, check out inet:setopts.

希望有所帮助。

这篇关于Erlang gen_tcp连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆