Send message to UDP Server

send message to UDP Server By linux shell

  1. send asc code message:
echo -n "hello" | nc -4u -w1 -p 10001 192.168.124.7 501  
  1. send hex string message:
echo -n -e "\x00\x00\x00\x00\x00\x08\x00\x0f\x00\x00\x00\x08\x01\x00" | nc -4u -w1 -p 10001 192.168.124.7 501  

send message to UDP Server By Nodejs

var dgram = require('dgram');  
var cmds =[  
  '00 00 00 00 00 08 00 0f 00 01 00 01 01 01',  //D6  第2路开
  '00 00 00 00 00 08 00 0f 00 01 00 01 01 00',  //D6  第2路关
  '00 00 00 00 00 08 00 0f 00 02 00 01 01 01',  //D5  第3路开
  '00 00 00 00 00 08 00 0f 00 02 00 01 01 00'   //D5  第3路关
];

var message = new Buffer(cmds[0].replace(/\s/g,''),'hex');  
//var message = "hello";  //asc code message
var client = dgram.createSocket('udp4');  
client.send(message, 0, message.length, 501, '192.168.124.7', function(err) {  
  console.log(err || "OK.");
  client.close();
});