Packets sometimes get concatenated

2019-06-21 08:32发布

问题:

I'm trying to make a simple server/application in Erlang.
My server initialize a socket with gen_tcp:listen(Port, [list, {active, false}, {keepalive, true}, {nodelay, true}]) and the clients connect with gen_tcp:connect(Server, Port, [list, {active, true}, {keepalive, true}, {nodelay, true}]). Messages received from the server are tested by guards such as {tcp, _, [115, 58 | Data]}.

Problem is, packets sometimes get concatenated when sent or received and thus cause unexpected behaviors as the guards consider the next packet as part of the variable.

Is there a way to make sure every packet is sent as a single message to the receiving process?

回答1:

Plain TCP is a streaming protocol with no concept of packet boundaries (like Alnitak said).

Usually, you send messages in either UDP (which has limited per-packet size and can be received out of order) or TCP using a framed protocol.

Framed meaning you prefix each message with a size header (usualy 4 bytes) that indicates how long the message is.

In erlang, you can add {packet,4} to your socket options to get framed packet behavior on top of TCP.

assuming both sides (client/server) use {packet,4} then you will only get whole messages.

note: you won't see the size header, erlang will remove it from the message you see. So your example match at the top should still work just fine



回答2:

You're probably seeing the effects of Nagle's algorithm, which is designed to increase throughput by coalescing small packets into a single larger packet.

You need the Erlang equivalent of enabling the TCP_NODELAY socket option on the sending socket.

EDIT ah, I see you already set that. Hmm. TCP doesn't actually expose packet boundaries to the application layer - by definition it's a stream protocol.

If packet boundaries are important you should consider using UDP instead, or make sure that each packet you send is delimited in some manner. For example, in the TCP version of DNS each message is prefixed by a 2 byte length header, which tells the other end how much data to expect in the next chunk.



回答3:

You need to implement a delimiter for your packets. One solution is to use a special character ; or something similar.

The other solution is to send the size of the packet first. PacketSizeInBytes:Body

Then read the provided amount of bytes from your message. When you're at the end you got your whole packet.

Nobody mentions that TCP may also split your message into multiple pieces (split your packet into two messages).

So the second solution is the best of all. But a little hard. While the first one is still good but limits your ability to send packets with special characters. But the easiest to implement. Ofc theres a workaround for all of this. I hope it helps.



标签: tcp erlang