Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 连接到Node.js中的udp套接字_Python_Node.js_Udp - Fatal编程技术网

Python 连接到Node.js中的udp套接字

Python 连接到Node.js中的udp套接字,python,node.js,udp,Python,Node.js,Udp,我正在编写一个javascript/node.js程序,从HL/Team Fortress 2服务器接收分数,但似乎无法接收游戏服务器发送的udp数据包。(我可以接收udp测试工具发送的udp数据包) 我发现一个python库可以工作,但它在从服务器接收数据之前使用socket.connect() Python snipplet(使用异步线程): 但在node.js中,我似乎无法连接到远程地址 到目前为止,我的代码是: var dgram = require('dgram') var serve

我正在编写一个javascript/node.js程序,从HL/Team Fortress 2服务器接收分数,但似乎无法接收游戏服务器发送的udp数据包。(我可以接收udp测试工具发送的udp数据包) 我发现一个python库可以工作,但它在从服务器接收数据之前使用socket.connect()

Python snipplet(使用异步线程):

但在node.js中,我似乎无法连接到远程地址

到目前为止,我的代码是:

var dgram = require('dgram')
var server = dgram.createSocket("udp4");
server.on('message', function (data, rinfo) {
    data = data.toString();
    if (data.startsWith('\xff\xff\xff\xff') && data.endsWith('\n\x00')) {
        console.log(data);
        Logparser(data);
    } else {
        console.log(data);
    }
});
server.bind(17015);

使用wireshark捕获的UDP数据包

您需要使用
addMembership()
方法加入多播组,在UDP中,您不连接到服务器,永远不会创建连接,也不能保证您将收到任何/所有数据包。如果您加入一个多播组并接收到到达网卡的具有该组id的数据包,则python方法名称会产生误导


如果我使用addMembership('127.0.0.1'),我会收到一个错误错误:node.js:201 throw e;//process.nextTick error,或第一次勾选时的“error”事件^error:getaddrinfo enoint at errnoException(dns.js:31:11)at Object.onanswer[as oncomplete](dns.js:140:16)通过谷歌搜索,当node.js找不到地址时,似乎会出现enoint错误代码。您应该了解多播地址不是IP地址。根据维基百科,有效的多播地址在224.0.0.0到239.255.255.255之间,因此127.0.0.1无效,因此出现错误。您似乎将UDP与多播混为一谈。UDP还有其他用途。
var dgram = require('dgram')
var server = dgram.createSocket("udp4");
server.on('message', function (data, rinfo) {
    data = data.toString();
    if (data.startsWith('\xff\xff\xff\xff') && data.endsWith('\n\x00')) {
        console.log(data);
        Logparser(data);
    } else {
        console.log(data);
    }
});
server.bind(17015);