[英]using node.js to listen for udp datagrams
I am working on a project where a C application sends udp datagrams from port 44044 and I would like to use node.js v0.10.20 to simply echo these packets to the console. 我正在一个项目中,其中C应用程序从端口44044发送udp数据报,并且我想使用node.js v0.10.20将这些数据包简单地回显到控制台。
I use the classical examples to connect: 我使用经典示例进行连接:
var dgram = require('dgram');
var port = 44044;
socket = dgram.createSocket('udp4');
socket.on('message', function (msg, info){
console.log(msg.toString());
});
socket.on('listening', function(){
var address = socket.address();
console.log("listening on :" = address.address + ":" + address.port);
});
socket.bind(port);
However, when I run the example, my C application complains that the port 44044 it uses for broadcasts is already in use. 但是,当我运行该示例时,我的C应用程序抱怨它用于广播的端口44044已被使用。 Conversely, if I start my C application first, the node.js application returns immediately with an error "Error: bind EADDRINUSE".
相反,如果我先启动C应用程序,则node.js应用程序将立即返回错误“错误:绑定EADDRINUSE”。
Now I understand that this means that I have two servers that are trying to serve on the same port. 现在,我知道这意味着我有两个试图在同一端口上服务的服务器。 But what I don't get is how can I get a node thread that will listen to udp broadcasts on port 44044?
但是我没有得到的是如何获得一个节点线程,该线程将侦听端口44044上的udp广播? Reading node.js documentation has not helped me solve this issue.
阅读node.js文档并没有帮助我解决此问题。
Try using SO_REUSEADDR
in your C program and in your node app use: 尝试在C程序和节点应用程序中使用
SO_REUSEADDR
:
socket = dgram.createSocket({ type: 'udp4', reuseAddr: true });
instead of: 代替:
socket = dgram.createSocket('udp4');
C app sends from port 44044. What port does it send to ? C应用程序从端口44044 发送 。它将发送到哪个端口? That's the port your
node.js
program needs to listen on. 那是您的
node.js
程序需要侦听的端口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.