0

I am a newbie to Node.js concepts. I am studying the use of Unix sockets as a means of communication between C and Node.js.

In C: I am sending 1000 iterations of a 100 byte long message.

In Node.js: I am receiving 4 data callbacks of size:

  • 1st callback: 10000
  • 2nd callback: 32200
  • 3rd callback: 32200
  • 4th callback: 25600

I understand that I am able to receive the complete data. However, how do I get this data in 1000 callbacks each with size 100 bytes (the same way I am sending it).

Code Reference

Server in C (Partial):

char buf[100];
int loop_count = 0;
memset(buf,0xA5,sizeof(buf));
while(loop_count < 1000) {
    rc = sizeof(buf);
    if (write(cl, buf, rc) != rc) {
        if (rc > 0) fprintf(stderr,"partial write");
        else {
            perror("write error");
            exit(-1);
        }
    }
    ++loop_count;
}

Client in Node.js:

var net = require('net');
var client = net.createConnection("temp");
#temp is the socket file name

client.on("connect", function() {
    console.log("Connection established");
});

client.on("data", function(data) {
   console.log("Found data "+data);
});    
Badacadabra
  • 8,043
  • 7
  • 28
  • 49
Ramdas
  • 3
  • 2

1 Answers1

0

Sockets buffer data, so while you write blocks of 100 bytes into a socket, a reader won't necessarily read the data in neat 100-byte blocks.

You can use a module like block-stream to split the data into 100-byte blocks for you:

const BlockStream = require('block-stream');
const net         = require('net');
const client      = net.createConnection('temp');
const bs          = new BlockStream(100);

let count = 0;

client.pipe(block).on('data', function(data) {
  count++;
  console.log('Found data: ', data);
}).on('end', function() {
   console.log('Read %s blocks', count);
});
robertklep
  • 198,204
  • 35
  • 394
  • 381
  • Thanks for your answer. I understand this resolves this query. However, is there a way to receive in the same way as sent by the sender (even if sizes of each send vary and the size is known to receiver). Like: [Sender ---> Size of Chunk 'x' over socket 1 -----> Receiver] [Sender ---> Chunk of size 'x' over socket 2 ---> Receiver] [Sender ---> Size of Chunk 'y' over socket 1 -----> Receiver] [Sender ---> Chunk of size 'y' over socket 2 ---> Receiver] I will be able to program socket 1 based on your suggestion. But how should I proceed with programming socket 2? – Ramdas May 03 '17 at 09:36
  • It's a tricky subject, because network data gets buffered by the OS, and during sending it may get split up in relatively arbitrary chunks. A common method of dealing with that is to prefix the block of data you're sending with a header that includes the size of the data; the format (and therefore, the size) of the header is known to both parties, and the receiver knows to expect a block of data of a particular size. But it will require some housekeeping on the receiving side, because one `data` event may contain multiple or partial packets. – robertklep May 03 '17 at 11:11