Features and applications of the 1.UDP protocol.
Although UDP transmission data is unreliable, no connection, but has the characteristics of fast transmission, in the transmission of audio, video, pictures, less data harmless, you can consider using UDP.
How UDP in the 2.node.js should be used.
The UDP module in node. js is ' Dgram ', referencing the UDP module and creating a UDP application instance in the following way:
var dgram = require (' Dgram '); var udp = Dgram.createsocket (' udp4 ');
The events corresponding to the Dgram instance are:
' Listening ': Start listening
' Message ': Receive a message
' Close ': Close connection
' ERROR ': Errors occurred
The corresponding methods are:
Send messages (Message message, message start offset, message end offset, port number, IP)
Close () Closes the connection
So how does the UDP send and receive end interact? This is illustrated by the following small example.
The sending side (sending a message to the receiving end, receiving the feedback from the sending side prints out):
var dgram = require (' Dgram '); var client = Dgram.createsocket (' udp4 '); var New Buffer (' Hi server '0, Message.length, 41234, ' 127.0.0.1 '); Client.on (function(msg) { console.info (' client Know server has got the message ')});
The receiving side (listens to the message, receives the message to send the end feedback):
var dgram = require (' Dgram '); var server = Dgram.createsocket (' udp4 '); Server.on (function() { Console.info ( ' server is listening ');}); Server.on (function(msg, rinfo) { console.info (' Get message: ' + msg + ' from ' + rinfo.address + ': ' + rinfo.port); var New Buffer (' I got message! from server '); 0, Message.length, Rinfo.port, rinfo.address);}); Server.bind (41234);
NODE.JS--UDP