I'm currently developing a UDP pinger, where the client should send a message (Ping) and receive pong back 10 times. However, the messages are sent 10 times at the same time, where the idea is to have the server send pong back after receiving ping, not having the terminal sending everything at once.
This is due to the for loop around the server.send, which i am aware of. I'm just not sure how i can get the server to only send a reply back after receiving a message, making the server record a different timing for each message. I hope my problem makes sense. I have so far come up with this:
var host = "127.0.0.1", port = 33333;
var dgram = require( "dgram" );
var buffer = require('buffer');
var server = dgram.createSocket( "udp4" );
var date1 = new Date()
var today = Buffer.from(date1)
server.on('listening', () => {
const address = server.address();
console.log(`server listening on port ` + 4000 + ' and address 127.0.0.1');
});
server.on( "message", function( msg, rinfo ) {
for (let message = 0; message < 10; message++){
const message = 'Ping'
server.send( message, 0, msg.length, rinfo.port, rinfo.address, date1 );
console.log( rinfo.address + ':' + rinfo.port + ' - ' + msg + ' . ' + date1 );
}});
server.on('error', (err) => {
console.log(`server error:\n${err.stack}`);
server.close();
});
server.on("close", function(){
console.log("\n Client is offline")
})
server.bind( port, host );
Client side:
var host = "127.0.0.1", port = 33334;
var dgram = require( "dgram" );
var buffer = require('buffer');
var client = dgram.createSocket( "udp4" );
var date1 = new Date()
var today = Buffer.from(date1)
console.log(date1)
client.on('listening', () => {
const address = client.address();
console.log(`client listening on port ` + 4000 + ' and address 127.0.0.1');
});
client.on( "message", function( msg, rinfo ) {
console.log( rinfo.address + ':' + rinfo.port + ' - ' + msg + ' - ' + date1 );
});
client.on('error', (err) => {
console.log(`server error:\n${err.stack}`);
server.close();
});
client.on("close", function(){
console.log("\n Client is offline")
})
client.bind( port, host ); //client listens to a port
var message = "Pong!"
client.send(message, 0, message.length, 33333, "127.0.0.1", date1 );
So far the code also prints out the time when each message is sent (I get the same time for all of the messages sent). Any ideas as to how i could maybe incorporate the round trip time after the 10 pings and pongs have been sent would be amazing.
Many thanks in advance!
You just need to count the number of times a ping is received and check if it equals ten:
var numPingPongs = 0;
server.on( "message", function( msg, rinfo ) {
if (numPingPongs < 10) {
const message = 'Ping'
server.send( message, 0, msg.length, rinfo.port, rinfo.address, date1 );
console.log( rinfo.address + ':' + rinfo.port + ' - ' + msg + ' . ' + date1 );
numPingPongs++;
} else {
// Reached ten ping pongs, do whatever here.
}
});
I assumed that you'd want to calculate latency or whatever on the server side, so I just wrote this for the server. But you can apply the logic to the client as well.