当net.Server接收超过1500字节(默认mtu)的数据时,将对数据包的每个片段执行“on data”事件。有没有办法在单个'on data'调用中接收整个数据包?
感谢。
答案 0 :(得分:2)
试试这个
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);