When sending multiple messages to a Node.js tcp socket, they get streamed as a single message

后端 未结 2 1291
广开言路
广开言路 2021-02-02 17:37

To show a simple example, I want to send multiple messages to a node.js tcp socket. I only want to send the second message when the first message is fully sent/drained. Howeve

2条回答
  •  悲哀的现实
    2021-02-02 18:31

    TCP does not send messages "separately". TCP is a stream protocol, which means that when you write bytes to the socket, you get the same bytes in the same order at the receiving end. There is no notion of "message boundaries" or "packets" anything of the sort.

    In your example, the callback is called when the socket layer accepts your data for transmission. It does not mean that the data has been successfully sent, or even that it has left your computer at all. In your callback, you send a second message, but the socket layer combines that with the first message for efficiency because it doesn't matter to TCP.

提交回复
热议问题