Node.js--udp

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.