【nodejs开发】nodejs实现socket网络通信

(本节内容如下:),在NodeJS中有三种socket:1. TCP,2. UDP,3. Unix域套接字。,UDP/datagram sockets,The dgram module provides an implementation of UDP datagram sockets.,nodejs接收端的运行截图如下:
【nodejs开发】nodejs实现socket网络通信,测试c++发送端的运行截图如下:
【nodejs开发】nodejs实现socket网络通信,例子1:,例子2:,例子3:,【nodejs开发】nodejs实现socket网络通信,测试接收端1结果如下:
【nodejs开发】nodejs实现socket网络通信 测试接收端2结果如下:
【nodejs开发】nodejs实现socket网络通信,

(本节内容如下:)

1、简介

在NodeJS中有三种socket:1. TCP,2. UDP,3. Unix域套接字。

UDP/datagram sockets

Class: dgram.Socket
Event: 'close'
Event: 'connect'
Event: 'error'
Event: 'listening'
Event: 'message'
socket.addMembership(multicastAddress[, multicastInterface])
socket.addSourceSpecificMembership(sourceAddress, groupAddress[, multicastInterface])
socket.address()
socket.bind([port][, address][, callback])
socket.bind(options[, callback])
socket.close([callback])
socket.connect(port[, address][, callback])
socket.disconnect()
socket.dropMembership(multicastAddress[, multicastInterface])
socket.dropSourceSpecificMembership(sourceAddress, groupAddress[, multicastInterface])
socket.getRecvBufferSize()
socket.getSendBufferSize()
socket.ref()
socket.remoteAddress()
socket.send(msg[, offset, length][, port][, address][, callback])
Note about UDP datagram size
socket.setBroadcast(flag)
socket.setMulticastInterface(multicastInterface)
Example: IPv6 outgoing multicast interface
Example: IPv4 outgoing multicast interface
Call results
socket.setMulticastLoopback(flag)
socket.setMulticastTTL(ttl)
socket.setRecvBufferSize(size)
socket.setSendBufferSize(size)
socket.setTTL(ttl)
socket.unref()
dgram module functions
dgram.createSocket(options[, callback])
dgram.createSocket(type[, callback])

2、UDP单播

The dgram module provides an implementation of UDP datagram sockets.

2.1 接收端

  • server.js
const dgram = require('dgram');
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(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
});

server.on('listening', () => {
  const address = server.address();
  console.log(`server listening ${address.address}:${address.port}`);
});

server.bind(41234);
// Prints: server listening 0.0.0.0:41234

nodejs接收端的运行截图如下: 【nodejs开发】nodejs实现socket网络通信

测试c++发送端的运行截图如下: 【nodejs开发】nodejs实现socket网络通信

2.2 发送端

例子1:

const dgram = require('dgram');

const message = Buffer.from('Some bytes');
const client = dgram.createSocket('udp4');
client.send(message, 41234, 'localhost', (err) => {
  client.close();
});

例子2:

const dgram = require('dgram');
const buf1 = Buffer.from('Some ');
const buf2 = Buffer.from('bytes');
const client = dgram.createSocket('udp4');
client.send([buf1, buf2], 41234, (err) => {
  client.close();
});

例子3:

const dgram = require('dgram');
const message = Buffer.from('Some bytes');
const client = dgram.createSocket('udp4');
client.connect(41234, 'localhost', (err) => {
  client.send(message, (err) => {
    client.close();
  });
});

3、UDP组播

3.1 发送端

const dgram = require('dgram');
const server = dgram.createSocket('udp4');

// const local_ip = "192.168.0.xx";
const multicast_ip = "225.0.0.100"; // 这里必须是一个组播地址(D类IP地址)

server.on('close', () => {
    console.log('close socket');
});

server.on('listening', () => {
    console.log('listening...' );
    server.setBroadcast(true);
    server.setMulticastTTL(128);
    server.addMembership(multicast_ip);

    setInterval(() => {
        send_msg();
    }, 2000);
});

server.on('message', (msg, rinfo) => {
    console.log(`receive client message from ${rinfo.address}: ${rinfo.port}: ${msg}`);
});

server.bind(); // 随机绑定本机一个端口

function send_msg() {
    var date = new Date();
    console.log(date.toLocaleDateString() + " " + date.toLocaleTimeString() + ': send a message.');
    server.send('hi, i am server...', '8062', multicast_ip);
}

【nodejs开发】nodejs实现socket网络通信

3.2 接收端

const dgram = require('dgram');
var client = dgram.createSocket('udp4');

// const local_ip = '192.168.0.xx';
const multicast_ip = "225.0.0.100"; // 这里必须是一个组播地址(D类IP地址)

client.on('close', () => {
    console.log('client closed');
});

client.on('error', (err) => {
    console.log('client error' + err);
});

client.on('listening', () => {
    console.log('client listening...');
    client.setBroadcast(true);
    client.setMulticastTTL(128);
    client.addMembership(multicast_ip);
});

client.on('message', (msg, rinfo) => {
    console.log(`receive server message from ${rinfo.address}: ${rinfo.port}: ${msg}`);
});
client.bind('8062'); // 监听组播数据的端口

测试接收端1结果如下: 【nodejs开发】nodejs实现socket网络通信 测试接收端2结果如下: 【nodejs开发】nodejs实现socket网络通信

文章版权声明

 1 原创文章作者:苦情,如若转载,请注明出处: https://www.52hwl.com/37183.html

 2 温馨提示:软件侵权请联系469472785#qq.com(三天内删除相关链接)资源失效请留言反馈

 3 下载提示:如遇蓝奏云无法访问,请修改lanzous(把s修改成x)

 免责声明:本站为个人博客,所有软件信息均来自网络 修改版软件,加群广告提示为修改者自留,非本站信息,注意鉴别

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023年7月15日 下午4:44
下一篇 2023年7月15日 下午4:44