nodejs实现socket网络通信

时间:2022-10-11 21:58:45


????socket网络通信相关文章编写如下:????

  1. ????Python实现socket网络通信????
  2. ????C++实现socket网络通信????
  3. ????Android实现socket网络通信????
  4. ????nodejs实现socket网络通信????

????node.js相关文章汇总如下????:

  1. ????【Web开发】nodejs实现socket网络通信????-
  2. ????【Web开发】Node.js实现Web服务器(http模块)????
  3. ????【Web开发】Node.js实现Web服务器(express模块)????
  4. ????【Web开发】Ubuntu系统手动搭建Node.js环境????
  5. nodejs实现socket网络通信



  6. 本节内容如下:


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,) => {
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实现socket网络通信

测试c++发送端的运行截图如下:

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,) => {
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实现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,) => {
console.log(`receive server message from ${rinfo.address}: ${rinfo.port}: ${msg}`);
});
client.bind('8062'); // 监听组播数据的端口

测试接收端1结果如下:

nodejs实现socket网络通信


测试接收端2结果如下:

nodejs实现socket网络通信

后记

如果你觉得这些文字有一点点用处,可以给作者点个赞;╮( ̄▽ ̄)╭
如果你感觉作者写的不咋地//(ㄒoㄒ)//,就在评论处留言,作者继续改进。o_O???
谢谢各位小伙伴们啦( ´ ▽ ‘)ノ ( ´ ▽ ` )っ!!!