nodejs开发nodejs实现socket网络通信
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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接收端的运行截图如下:
测试c++发送端的运行截图如下:
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);
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结果如下: 测试接收端2结果如下:
以上是关于nodejs开发nodejs实现socket网络通信的主要内容,如果未能解决你的问题,请参考以下文章
如何允许其他网络连接到服务器 nodejs socket io
在nodejs中使用socket.io和net socket
Spring Boot 与 nodejs websocket 通信