开发者

limitation of the reception buffer

I established a connection with a client this way:

gen_tcp:listen(1234,[binary,{packet,0},{reuseaddr,true},{active,false},{recbuf,2048}]).

This code performs message processing:

loop(Socket)->
    inet:setops(Socket,[{active,once}],
    receive
        {tcp,Socket,Data}->
            handle(Data),
            loop(Socket);
        {Pid,Cmd}->
            gen_tcp:send(Socket,Cmd),
            loop(Socket);
        {tcp_close,Socket}->
            % ...
end.

My OS is Windows. When the size of the message is 1024 bytes, I lose bytes in Data. The server sends ACK + FIN to the client.

I believe that the Erlang is limited 开发者_StackOverflow中文版to 1024 bytes, therefore I defined recbuf.

Where the problem is: Erlang, Windows, hardware?

Thanks.


You may be setting the receive buffer far too small. Erlang certainly isn't limited to a 1024 byte buffer. You can check for yourself by doing the following in the shell:

{ok, S} = gen_tcp:connect("www.google.com", 80, [{active,false}]),
O = inet:getopts(S, [recbuf]),
gen_tcp:close(S),
O.

On Mac OS X I get a default receive buffer size of about 512Kb.


With {packet, 0} parsing, you'll receive tcp data in whatever chunks the network stack chooses to send it in, so you have to do message boundary parsing and buffering yourself. Do you have a reliable way to check message boundaries in the wire protocol? If so, receive the tcp data and append it to a buffer variable until you have a complete message. Then call handle on the complete message and remove the complete message from the buffer before continuing.

We could probably help you more if you gave us some information on the client and the protocol in use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜