Reputation: 107
I am using tcp to transfer data. The server code is written by C, and client code is written by nodejs. When I send one buffer, sometimes the client will receive two parts of this buffer, the console.log function will trigger twice, but sometimes it works well. following are nodejs code and C code. nodejs code:
var client = new net.Socket();
client.on('data', function(data) {
console.log('data:', data)
});
C code:
send(socket_file, buffer, strlen(buffer),0)
Upvotes: 3
Views: 129
Reputation: 399743
This is typical for TCP, which is a stream-oriented (as opposed to packet-oriented like UDP) protocol after all.
There's no guarantee that one write to the network equals one read at the other end, multiple writes may be delivered together and single writes may be split.
You must add an applicaton-level message protocol.
Upvotes: 2