As we all know, UDP data transmission methods include unicast, multicast, and broadcast.
Among them, unicast mode is the most commonly used, so I won’t talk about it here, you can search it online.
The following example mainly implements a simple UDP multicast method. UDP data can be sent and received normally.
Server side code
const dgram = require('dgram'); const server = ('udp4'); // const local_ip = "192.168."; const multicast_ip = "225.0.0.100"; // This must be a multicast address (class D IP address) ('close', ()=>{ ('close socket'); }); ('listening', ()=>{ ('listening...'); (true); (128); (multicast_ip); setInterval(()=>{ send_msg(); }, 2000); }); ('message', (msg, rinfo)=>{ (`receive client message from ${}: ${}: ${msg}`); }); (); // Randomly bind a port of the machine function send_msg(){ ('send msg'); ('hi, im server...', '8062', multicast_ip); }
Client side code:
const dgram = require('dgram'); var client = ('udp4'); // const local_ip = '192.168.'; const multicast_ip = "225.0.0.100"; // This must be a multicast address (class D IP address) ('close', ()=>{ ('client closed'); }); ('error', (err) =>{ ('client error' + err); }); ('listening', ()=>{ ('client listening...'); (true); (128); (multicast_ip); }); ('message', (msg, rinfo) => { (`receive server message from ${}: ${}: ${msg}`); }); ('8062'); // Port that monitors multicast data
refer to:
https:///article/
/questions/14130560/nodejs-udp-multicast-how-to
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.