0

I'm now trying to send bytes continuously from node.js(server) to Android(client). Let me show the code example.

var net = require('net');
var server = net.createServer(function(c){
    c.on('data', function(data){
        if(data == 'foo'){
            for(var i = 1; i <= 255; i++){
                var byteData = makeBytedata();
                c.write(byteData);
                wait(100)
            }
        }
    });
});

This code does not works fine because it sometimes combines byteData to one packet. Does anyone have solution to send bytes separately?

6
  • It's a little hard to understand what you're asking, but you may need to read this to turn off the Nagle algorithm delay that combines separate writes into a common packet: nodejs.org/api/net.html#net_socket_setnodelay_nodelay. Also, a wait(100) in a nodejs server is probably the wrong design since nodejs is single threaded, it cannot do anything else during a hard-wired wait loop. Commented Jan 28, 2016 at 7:26
  • Thanks. I insert c.setNoDelay(true) before for loop, but bytes combined. Commented Jan 28, 2016 at 8:42
  • 1
    What does "bytes combined" mean? In TCP, when the client on the other end of the connection reads from the socket, it will likely read all bytes that are available. That's how TCP works. It isn't a datagram protocol. Commented Jan 28, 2016 at 8:43
  • "bytes combined" means, for example, the data of i = 45 and i = 46 is in one tcp packet. The packets from i = 1 to 44 is no problem. I found it with wireshark. If the tcp does not send data separately like zangw said, it might be better to assemble all bytes first before analyze it. My code is analyze bytes first now. Commented Jan 28, 2016 at 9:40
  • @yosh, you mean i = 1 tp 44, all of them are separated packets from wireshark. However, for i = 45, 46, they are combined into one packet? BTW, all byteDatas are same data or different? Commented Jan 28, 2016 at 9:45

1 Answer 1

1

net.createServer create TCP server, 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.

One work around: define a format for your message, so that your client can determine the beginning and end of a message within the socket stream. For example, you could use a \n to mark the end of a message.

   for(var i = 1; i <= 255; i++){
        var byteData = makeBytedata();
        c.write(byteData + '\n');
    }

Then the client could separate them by \n.

The other way could be to use UDP/Dgram

var dgram = require("dgram"),
    server = dgram.createSocket('udp4');

server.on("message", function(msg, rinfo) {
    // send message to client 
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.