Simple question...
This code ..
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).
is very clear except that I don't quite understand what [binary, {packet,0}] means ? Any one cares to explain ?
MadSeb
{packet,0} is used to indicate that TCP data is delivered directly to the application in an unmodified form.
binary means that received packet is delivered as a binary. (but you can still use gen_tcp:send with a message like "message")
As per the gen_tcp:connect documentation:
[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 thepacket
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.Hope that helps.