3

When a net.Server receives data that exceeds 1500 bytes (default mtu), the 'on data' event is executed with each fragment of the packet. Is there a way to receive the whole packet in a single 'on data' call?

Thanks.

4
  • Why ? Just concatenate the chunks if you need it in one piece. Commented Jul 21, 2014 at 6:39
  • There are hundred of sources sending data, the source_id is in the begin of packet. If a packet is fragmented, I can't identify the source of the following chunks. Commented Jul 21, 2014 at 6:47
  • Can you show your code ? Usually you work on sockets, you don't handle packets just like this... Commented Jul 21, 2014 at 6:49
  • The code is too large. The socket passed to "onconnection" event have to handle several event types. I can't simply concatenate the events after receive them. Commented Jul 21, 2014 at 7:15

1 Answer 1

2

Try this

var sys     = require('sys');
var net     = require('net');;

var socktimeout = 600000;
var svrport = your_port;

var svr = net.createServer(function(sock) {
  var mdata = new Buffer(0);
    //sys.puts('Connected: ' + sock.remoteAddress + ':' + sock.remotePort); 
     sock.setTimeout(socktimeout,function(){
            sock.end("timeout");
            sock.destroy();
        });

    sock.on('data', function(data) {  


        if(mdata.length != 0)
        {
          var tempBuf = Buffer.concat([mdata, data]);
          mdata = tempBuf;
        }
        else
        {
          mdata = data;
        }

        var len=got_your_Packget_length(mdata);
         
      if(mdata.length == len)
      {
        do_your_job(mdata)
        mdata = new Buffer(0);
      }


    });
 


    sock.on('error', function(err) { // Handle the connection error.
        sys.puts('error: ' + err +'\n');
    });
});
 
svr.listen(svrport);

Sign up to request clarification or add additional context in comments.

1 Comment

I believe this is useless if you got no idea how large the data package is. I think it should be possible to check the datagram's ethernet fragment length, frag_flag and offset. For a better understanding: stackoverflow.com/questions/15999739/…

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.