1

I am trying to understand dgram with a small example of client/server. However, it seems that I can only send 1 message per run of the client. If I try to send multiple messages like in below client code, nothing gets sent.

Server code:

var PORT = 16501;
var HOST = '127.0.0.1';

var dgram = require('dgram');
var server = dgram.createSocket('udp4');

server.on('listening', function () {
    var address = server.address();
    console.log('UDP Server: ' + address.address + ":" + address.port);
});

server.on('message', function (message, remote) {
    console.log('Received: ' + remote.address + ':' + remote.port +' - ' + message);

});

server.bind(PORT, HOST);

Client code:

var PORT = 16501;
var HOST = '127.0.0.1';

var dgram = require('dgram');

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

var i;

for(i=0;i<10;i++) {
    client.send('Test Message', 0, 12, PORT, HOST, function(err, bytes) {
        if (err) throw err;
        console.log('Send: ' + HOST +':'+ PORT);
    });
}
client.close();

This client code works but can only send 1 message.

var PORT = 16501;
var HOST = '127.0.0.1';

var dgram = require('dgram');

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

client.send('Test Message', 0, 12, PORT, HOST, function(err, bytes) {
    if (err) throw err;
    console.log('Send: ' + HOST +':'+ PORT);
    client.close();
});

How can I make it so that it can send packets one after the other continuously?

user846016
  • 400
  • 5
  • 10

1 Answers1

0

You are closing the socket connection before the operations ends. I can suggest a flag to close it only after all the messages was sent, something like:

var totalReq = 10;
for (var i = 0; i < totalReq; i++) {
    sendMessage(i, totalReq);
}

function sendMessage(index, total) {
    client.send('Test Message', 0, 12, PORT, HOST, function(err, bytes) {
        if (err) throw err;
        console.log('Send: ' + HOST + ':' + PORT);
        // close the connection only after the last request
        if (index == total - 1) {
            client.close();
        }
    });
}
BrTkCa
  • 4,703
  • 3
  • 24
  • 45
  • This partially works in that all messages are now visible on the server. However, it so happens that all messages pop up on the server at the same time right after executing client.close() here. Hence, if the totalReq is very high value here, nothing gets displayed on server for a long time. Is it possible to have it such that the server displays messages as and when it arrives? – user846016 Jan 29 '19 at 16:25
  • @user846016 actually the server received the message on the time that was sent, the point is that the for loop is running very fast than client.send. For test purposes you can set a delay in each send command wrapping with setTimeout. The root issue is that the *send* feature is async, so after the for loop it will close the connection and I shown how to prevent that. But, the time that the server receive the send statement is defined by the socket. – BrTkCa Jan 29 '19 at 17:11
  • Ok. Now I got it. It seems that the send call is async but still runs on the same thread. Unless we stop blocking the thread, it never gets the chance to send them. If instead I call the sendMessage with setInterval instead of a for loop, everything works as expected. Thanks. – user846016 Jan 30 '19 at 01:15
  • Great @user846016. Glad to help :) – BrTkCa Jan 30 '19 at 10:15