簡易使用NodeJS建立UDP Server
如果傳輸用ASCII CODE,可以搭配範例hextool.js來處理轉換的問題。
npm install udp4 --save
const server = dgram.createSocket('udp4'); server.on('error', (err) => { console.log(`server error:\n${err.stack}`); server.close(); }); server.on('message', (msg, rinfo) => { console.log(msg); //把收到的送回去 server.send(msg, 0, msg.length, rinfo.port, rinfo.address, function(err, bytes) { if(err) { console.log(err); return; } }); }); server.on('listening', () => { var address = server.address(); console.log(`server listening ${address.address}:${address.port}`); }); server.bind(2009);
酷米 © All Rights Reserved.