Erlang server, Java client - TCP messages get split?

前端 未结 3 1840
广开言路
广开言路 2020-12-07 04:50

As the title says, I have a server written in Erlang, a client written in Java and they are communicating through TCP. The problem that I am facing is the fact that gen_tcp:

3条回答
  •  猫巷女王i
    2020-12-07 05:03

    This makes me wonder if it is something that can be fixed on the Java side?

    No, absolutely not. Regardless of why you don't happen to see the problem with an Erlang client, if you aren't putting any sort of "message boundary" indication into the protocol, you will not be able to reliably detect whole messages. I strongly suspect that if you send a very large message with the Erlang client, you'll still see split messages.

    You should either:

    • Use some sort of "end of message" sequence, e.g. a 0 byte if that wouldn't otherwise come up in your messages.
    • Prefix each message with the length of the message.

    Aside from that, you aren't clearly differentiating between bytes and text at the moment. Your Java client is currently silently ignoring the top 8 bits of each char, for example. Rather than using DataOutputStream, I would suggest just using OutputStream, and then for each message:

    • Encode it as a byte array using a specific encoding, e.g.

      byte[] encodedText = text.getBytes(StandardCharsets.UTF_8);
      
    • Write a length prefix to the stream (possibly in a 7-bit-encoded integer, or maybe just as a fixed width, e.g. 4 bytes). (Actually, sticking with DataOutputStream would make this bit simpler.)

    • Write the data

    On the server side, you should "read a message" by reading the length, then reading the specified number of bytes.

    You can't get around the fact that TCP is a stream-based protocol. If you want a message-based protocol, you really do have to put that on top yourself. (I'm sure there are helpful libraries to do this, of course - but you shouldn't just leave it up to TCP and hope.)

提交回复
热议问题