NodeJS DGRAM Module Broadcast + Multicast Implementation Example

Native IP address is 10.8.93.80,

The specific information is shown in the figure above nodejs dgram模块广播+组播的实现示例

, according to the calculation, I know my broadcast address It should be 10.8.93.255, not 10.8.93.254.



Const DGRAM = Require (‘DGRAM’ ); const server = dgram.createsocket (‘udp4’); const multicastaddr = ‘224.100.100.100’; server.on (‘close’, () => {console.log (‘socket is closed “;}); Server.on (‘Error’, (ERR) => {Console.log (ERR);}; server.on (‘listening “, () => {console.log (‘ socket is listening … ‘ ); server.addmembership (multicastaddr); // does not write server.setBroadcast (TRUE); server.setMultiTCastTTL (128); setInterval (() => {sendMSG ();}, 1500);}; server. ON (‘Message’, (MSG, RINFO) => {Console.log (`Receive Client Message from $ {rinfo.address}: $ {rinfo.port}`);}); function sendmsg () {Console.log(‘sending’); server.send (‘Hello, I am the server broadcast message’, 8061, ’10 .8.93.255 ‘); server.send (‘ Hello, I am a server multicast message ‘, 8061, multicastaddr);} server.bind (‘8060’); // cannot be multicast after filling in IP
   
Client.js

Const Dgram = Require (‘DGRAM’); const client = DGRam.createsocket (‘udp4’); const multicastaddr = ‘224.100.100.100’; client.on (‘close’, () = > {Console.log (‘socket Close’);}); Client.on (‘Error’, (ERR) => {Console.log (ERR);}; Client.on (‘Listening “, () => {Console.log (‘socket is listening …’); client.addmembership (multicastaddr);}; client.on (‘Message’, (MSG, RINFO) => {Console.log (`Receive Server Message from $ {rinfo.address}: $ {rinfo.port}: $ {msg} `);}; client.bind (8061); // must bind your local area network IP or not fill, fill in Localhost is not
 My server is the binding 8060 port, 8061 ends of the broadcast addressThe port is transmitted, deliberately changed to the local client to receive it, otherwise it is not occupied by the port.   
The above is all the content of this article, I hope to help everyone, I hope everyone will support Tumi Clouds.
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment