Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang gen_tcp connect question

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

like image 787
MadSeb Avatar asked Apr 24 '11 20:04

MadSeb


1 Answers

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 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.

Hope that helps.

like image 143
OJ. Avatar answered Oct 23 '22 22:10

OJ.