0

我试图弄清楚节点 js 发送 n 个数据包需要多长时间。我的最终目标是找到网络的传输延迟。我做了一个递归函数,所以下一个数据包在前一个数据包完成之前不会发送。到目前为止,这是我的代码。我做得对吗?

var sizes=[200,400,600,800,1000];



// NOTE: the port is different
var host = "127.0.0.1", port = 33334;

var dgram = require( "dgram" );

var client = dgram.createSocket( "udp4" );

/*
client.on( "message", function( msg, rinfo ) {
    var end = new Date().getTime();
    var total = end - start;

    console.log( "The packet came back in "+ total+" MiliSeconds");
});
*/

// client listens on a port as well in order to receive ping
client.bind( port, host );

var number_packets = 50;
var packet_size = 100;

var count = 0;
var end;

var start = new Date().getTime();

var message = new Buffer(packet_size);


send(message);


function send(message) {

    client.send(message, 0, message.length, 33333, "127.0.0.1", function(err, bytes){

        count++;
        console.log("pkt #: "+count);
        if(count < number_packets)
            send(message);

        if( count >= number_packets ) {

            end = new Date().getTime();
            var total = end - start;
            console.log("total: "+total);
        }

    } );




}

有一个 server.js 接收数据包并向控制台写入已收到数据包。

4

0 回答 0