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.
var dgram = require('dgram'); var client = dgram. createSocket('udp4'); client. send('Hello World!
The node:dgram module provides an implementation of UDP datagram sockets.
WebSocket is built on TCP. However, UDP is much preferred over TCP for networking in realtime multiplayer games. Refer to the awesome visualizations in Gaffer On Games: Deterministic Lockstep to see why. udp-ws is a UDP version of WebSocket built on WebRTC, which allows peer-to-peer UDP communication in the browser.
DatagramSockets are Java's mechanism for network communication via UDP instead of TCP. Java provides DatagramSocket to communicate over UDP instead of TCP. It is also built on top of IP. DatagramSockets can be used to both send and receive packets over the Internet.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With