• Erlang网络编程-packet参数
    时间:2008-10-22   作者:佚名   出处:互联网

    gen_tcp:listen(Port, Options),Options 为一个参数列表

    之前介绍过 {active, Boolean} 这个 opt,现在介绍一下 {packet, PacketType}


        {packet, PacketType} (TCP/IP sockets)
        Defines the type of packets to use for a socket. The following values are valid:
        raw | 0
        No packaging is done.
        1 | 2 | 4
        Packets consist of a header specifying the number of bytes in the packet, followed by that number of bytes. The length of header can be one, two, or four bytes; the order of the bytes is big-endian. Each send operation will generate the header, and the header will be stripped off on each receive operation.
        asn1 | cdr | sunrm | fcgi | tpkt | line
        These packet types only have effect on receiving. When sending a packet, it is the responsibility of the application to supply a correct header. On receiving, however, there will be one message sent to the controlling process for each complete packet received, and, similarly, each call to gen_tcp:recv/2,3 returns one complete packet. The header is not stripped off.
        The meanings of the packet types are as follows:
        asn1 - ASN.1 BER,
        sunrm - Sun’s RPC encoding,
        cdr - CORBA (GIOP 1.1),
        fcgi - Fast CGI,
        tpkt - TPKT format [RFC1006],
        line - Line mode, a packet is a line terminated with newline, lines longer than the receive buffer are truncated.


    gen_tcp:listen(Port, [binary, {active, true}, {packet, 2}])

    {tcp, Socket, Binary} ->

    这样主动发送过来的Binary,就是一个完整的Body,不包含头部的Size信息,也无需自行处理tcp packet的完整性问题。

    此外它还支持 asn1 | cdr | sunrm | fcgi | tpkt | line 这些协议,大大方便开发。


        Erlang的 tcp_gen:listen 参数中有一个 backlog 据说是 undocument 的参数(参考这篇 a fast web server demostrating some undocument erlang feature),看来 ericsson 也会在 opensource 的 erlang 中留一手啊。呵呵。