javascript 节点 JS:Ping 服务器和客户端 UDP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13350640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:31:07  来源:igfitidea点击:

Node JS: Ping Server & Client UDP

javascriptnode.js

提问by Carlos Mendoza

I want to create a small ping script. I am a beginner in node js. My ultimate small goal is to have the client ping the server. I want the server to acknowledge the packet by logging the message in the console and I want it to send back the same packet/message back to the client.

我想创建一个小的 ping 脚本。我是 node js 的初学者。我的最终小目标是让客户端 ping 服务器。我希望服务器通过在控制台中记录消息来确认数据包,并且我希望它向客户端发回相同的数据包/消息。

This is what I have so far:

这是我到目前为止:

Server:

服务器:

var PORT = 33333;
var HOST = '127.0.0.1';

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

server.on('listening', function () {
    var address = server.address();
    console.log('UDP Server listening on ' + address.address + ":" + address.port);
});

server.on('message', function (message, remote) {

    console.log(remote.address + ':' + remote.port +' - ' + message);
    // I added a server.send but it gave me an infinite loop in the server console

});

server.bind(PORT, HOST);

Client:

客户:

var PORT = 33333;
var HOST = '127.0.0.1';

var dgram = require('dgram');
var message = new Buffer('My KungFu is Good!');

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

client.on('message', function (message, remote) {

    console.log("The packet came back");

});


client.send(message, 0, message.length, PORT, HOST, function(err, bytes) {
    if (err) throw err;
    console.log('UDP message sent to ' + HOST +':'+ PORT);
    count++;
});

UPDATE:

更新:

Thanks! That really helped. But I have another question. Let's say I want to send the packet in a specific number of bytes. I would replace 'message.length' with 1000 for 1kb right? But I get an error 'throw new Error('Offset + length beyond buffer length');'

谢谢!那真的很有帮助。但我还有一个问题。假设我想以特定字节数发送数据包。我会将 'message.length' 替换为 1000 为 1kb,对吗?但是我收到一个错误'throw new Error('Offset + length Beyond buffer length');'

I don't quite understand why.

我不太明白为什么。

回答by freakish

One thing is sending data and the other thing is receiveing it. Since UDP protocol is bidirectional, then actually there is no strict difference between client and server. So your server and client code will be almost the same, the difference is that actually one will send packets and other will only respond. Also note that you have an infinite loop, because your probably using .sendwith PORTand HOSTvariables and you have to send to different host/port pair.

一件事是发送数据,另一件事是接收数据。由于UDP协议是双向的,那么实际上客户端和服务器之间没有严格的区别。所以你的服务器和客户端代码几乎相同,区别在于实际上一个会发送数据包而另一个只会响应。另请注意,您有一个无限循环,因为您可能使用.sendwithPORTHOSTvariables 并且您必须发送到不同的主机/端口对。

Here's an example:

下面是一个例子:

server

服务器

var host = "127.0.0.1", port = 33333;

var dgram = require( "dgram" );

var server = dgram.createSocket( "udp4" );

server.on( "message", function( msg, rinfo ) {
    console.log( rinfo.address + ':' + rinfo.port + ' - ' + msg );
    server.send( msg, 0, msg.length, rinfo.port, rinfo.address ); // added missing bracket
});
server.bind( port, host );

client

客户

// NOTE: the port is different
var host = "127.0.0.1", port = 33334;

var dgram = require( "dgram" );

var client = dgram.createSocket( "udp4" );

client.on( "message", function( msg, rinfo ) {
    console.log( "The packet came back" );
});

// client listens on a port as well in order to receive ping
client.bind( port, host );

// proper message sending
// NOTE: the host/port pair points at server
var message = new Buffer( "My KungFu is Good!" );
client.send(message, 0, message.length, 33333, "127.0.0.1" );

回答by hytromo

That's an old question, but you don't really need to bind the client to a portas well, you can use the not-closed underlying socket to reply immediately

这是一个老问题,但您实际上并不需要将客户端绑定到端口,您可以使用未关闭的底层套接字立即回复

server.js

服务器.js

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

const reply = new Buffer('pong');

server.on('message', function (message, remote) {
    console.log('Got', message.toString());
    server.send(reply, 0, reply.length, remote.port, remote.address);
});

server.bind(6790, '0.0.0.0');

client.js

客户端.js

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

const reply = new Buffer('ping');

client.on('message', function (message, remote) {
    console.log('Got', message.toString());
    client.send(reply, 0, reply.length, 6790, '0.0.0.0');
});

client.send(reply, 0, reply.length, 6790, '0.0.0.0');

Start the server and then the client for an endless ping-pong game.

启动服务器,然后启动客户端以进行无休止的乒乓游戏。