Node.js build UDP service

UDP Introduction

  • User Datagram Protocol, referred to as UDP, also known as the User Datagram Protocol.
  • And TCP as a transport layer network is located, for processing data packets.
  • UDP is a connectionless biggest feature
  • UDP transmission speed
  • UDP is not reliable data transmission
    • Does not provide data packet grouping, assembly and disadvantages can not sort the data packet, that is, when the message is sent, it is not known whether the complete security arrived.
    • Reliability responsibility of the application layer
  • Support one to one communication, also supports many communication
  • Many key Internet applications use UDP
    • Services such as Domain Name System DNS, TFTP Trivial File Transfer Protocol, DHCP dynamic host configuration protocol, etc.
  • UDP is suitable for relatively high speed, without rigorous application data quality requirements
    • Such as streaming media, real-time multiplayer games, audio and video in real time

Three modes of transmission UDP

  • UDP Unicast
  • Unicast is the destination address is a single target mode of transmission
  • Address range: 0.0.0.0 ~ 223.255.255.255

  • UDP broadcast
  • Limited broadcast: it is not forward routing, network field IP address is the address 255.255.255.255 1
  • Direct broadcast: will be forwarded routing, network field defines the IP address of the network, the host field is usually all 1, such as 192.168.10.255

  • UDP Multicast
    • Multicast is to transmit information to a group of destination address
    • Address range: 224.0.0.0 ~ 239.255.255.255
    • Group 224.0.0.0 ~ 224.0.0.255 permanent address, 244.0.0.0 reservation is not allocated, for other routing protocols.
    • 224.0.1.0 ~ 224.0.1.255 multicast address is a public, it can be used for internet.
    • 224.0.2.0 ~ 238.255.255.255 available to the user multicast address (Temporary group), the whole network is valid, the application needs.
    • 239.0.0.0 ~ 239.255.255.255 local multicast management, only valid for a specific local scope.

The Node module dgram

Node provides a module for building dgram UDP services we
use the module to create a very simple UDP sockets, UDP sockets once created, that can send data as a client, it can be accepted as server data.

UDP Unicast example:

const dgram = require('dgram')

const server = dgram.createSocket('udp4')

// 当绑定端口好启动成功后触发
server.on('listening', () => {
  const address = server.address()
  console.log(`client running ${address.address}: ${address.port}`)
})

// 当收到消息时触发
server.on('message', (msg, remoteInfo) => {
  console.log(`来自客户端消息:【${msg}】 from ${remoteInfo.address}: ${remoteInfo.port}`)
  server.send('word', remoteInfo.port, remoteInfo.address)
})

// 发生异常触发
server.on('error', err => {
  console.log('server error', err)
})

// 绑定端口号
server.bind(20000)
const dgram = require('dgram')

const client = dgram.createSocket('udp4')

// 当绑定端口好启动成功后触发
client.on('listening', () => {
  const address = client.address()
  console.log(`client running ${address.address}: ${address.port}`)
})

// 发送消息
client.send('hello', 20000, 'localhost')

// 当收到消息时触发
client.on('message', (msg, remoteInfo) => {
  console.log(`来自服务端消息:【${msg}】 from ${remoteInfo.address}:${remoteInfo.port}`)
})

// 发生异常触发
client.on('error', err => {
  console.log('client error', err)
})

// 绑定端口号
// client.bind(20001)

UDP broadcast Example:

Send a message:

const dgram = require('dgram')

const server = dgram.createSocket('udp4')

// 当绑定端口好启动成功后触发
server.on('listening', () => {
  const info = server.address()
  console.log(`server running ${info.address}: ${info.port}`)
  // 开启广播模式
  server.setBroadcast(true)
  // 案例: 每隔两秒发送一条消息
  let a = 1
  setInterval(() => {
    // 直接地址 可以经过路由转发
    // 受限地址 255.255.255.255 不会经过路由转发,只在当前连接的路由器局域网中
    server.send(`测试数据${a++}`, 20001, '255.255.255.255')
  }, 2000)
})


// 绑定端口号
server.bind(20000)

Receive messages:

const dgram = require('dgram')

const client = dgram.createSocket('udp4')

// 当收到消息时触发
client.on('message', (msg, remoteInfo) => {
  console.log(`收到消息:【${msg}】 from ${remoteInfo.address}:${remoteInfo.port}`)
})

// 绑定端口号
client.bind(20001)

UDP Multicast example

send data:

//  组播
const dgram = require('dgram')

const server = dgram.createSocket('udp4')

// 当绑定端口好启动成功后触发
server.on('listening', () => {
  const info = server.address()
  console.log(`server running ${info.address}: ${info.port}`)
  // 案例: 每隔两秒向组播发送一条消息
  let a = 1
  setInterval(() => {
    server.send(`测试数据${a++}`, 20001, '224.1.1.101')
  }, 2000)
})

// 绑定端口号
server.bind(20000)

Receive data:

//  组播
const dgram = require('dgram')

const client = dgram.createSocket('udp4')

client.on('listening', () => {
  // 加入组播的组
  client.addMembership('224.1.1.101')
})

// 当收到消息时触发
client.on('message', (msg, remoteInfo) => {
  console.log(`收到消息:【${msg}】 from ${remoteInfo.address}:${remoteInfo.port}`)
})

// 绑定端口号
client.bind(20001)

Guess you like

Origin www.cnblogs.com/liea/p/11832547.html