Monday, 22 March 2021

How to send udp packages using nodejs?

Well, I have a small route that should send an UDP package and print a confirmation. According to the documentation on the node, the following should work fine:

const dgram = require('dgram');

export async function sendUDP(sess, parameters: {}, res) {
    const client = dgram.createSocket('udp4');
    client.send('Hello World!',0, 12, 12000, '127.0.0.1', function(err, bytes) {
        client.close();
    });

    //res is the response object from express
    return res.send("Send udp packet");
}

It should send an UDP request to port 12000 on the callback IP (local machine). And also send a reply that the UDP packet has been sent.

I notice the reply Send udp packet received at postman when I post to the correct URL. So that is working.

However, the UDP packages seem to be lost, using tcpdump on my local ubuntu results in nothingness:

sudo tcpdump -n udp port 12000

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on wlp5s0, link-type EN10MB (Ethernet), capture size 262144 bytes

0 packets captured
0 packets received by filter
0 packets dropped by kernel

(While tcpdump has been kept running during the test of the functions of course). Where do the UDP requests go? What happened?

I wish to have a life logger so that I can test nodejs applications using udp easily.



from How to send udp packages using nodejs?

No comments:

Post a Comment