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.
You need to tell tcpdump the interface it needs to listen.
tcpdump -i lo udp port 12000
lo is the interface for localhost.
More information in this link
You can use wireshark if you want to save the traffic log.
I use '!(udp.port == 53 || tcp.port == 53) && udp' as a filter to look only to udp packet.
I add this code to my server and send the udp packet there to print the content
const dgram = require('dgram');
const serverUDP = dgram.createSocket('udp4');
serverUDP.on('error', (err) => {
console.log(`serverUDP error:\n${err.stack}`);
serverUDP.close();
});
serverUDP.on('message', (msg, rinfo) => {
console.log(`serverUDP got: ${msg} from ${rinfo.address}:${rinfo.port}`);
});
serverUDP.on('listening', () => {
const address = serverUDP.address();
console.log(`serverUDP listening ${address.address}:${address.port}`);
});
serverUDP.bind(3001);
It is the code sample from the node documentation