UDP绑定:地址已在使用中 [英] UDP bind: Address already in use
问题描述
我不知道 node.js
。
我发送 UDP
数据包,我想在 C ++
中的 node.js
中显示它们。
I am sending UDP
packets in C++
and I want to show them in node.js
.
问题是两者都试图占用IP /端口。根据我运行的一个,另一个失败。我应该如何解决这个问题?
The problem is that both try to occupy the IP/port. Depending on which one I run, the other one fails. How should I fix this problem?
main.cpp
// g++ main.cpp -o main -std=c++11 -lboost_system -pthread
#include <iostream>
#include <stdio.h>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::udp;
int main(int argc, char* argv[])
{
const int port=1414;
const std::string ip="127.0.0.1";
boost::asio::io_service io_service;
udp::resolver resolver(io_service);
udp::endpoint client_endpoint = *resolver.resolve({udp::v4(), ip, std::to_string(port)});
udp::socket socket(io_service, udp::endpoint(udp::v4(), port));
boost::asio::socket_base::reuse_address option(true);
socket.set_option(option);
std::string data;
for(long i=0;true;i++)
{
data=std::to_string(i+1)+"th packet";
socket.send_to(boost::asio::buffer(data.c_str(), data.length()+1), client_endpoint);
usleep(10);
}
return 0;
}
app.js
var PORT = 1414;
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);
});
server.bind(PORT, HOST);
如果我运行 C ++
js
一,错误是:
If I run the C++
file and then the js
one, the error is this:
events.js:160
throw er; // Unhandled 'error' event
^
Error: bind EADDRINUSE 127.0.0.1:1414
at Object.exports._errnoException (util.js:1012:11)
at exports._exceptionWithHostPort (util.js:1035:20)
at dgram.js:221:18
at _combinedTickCallback (internal/process/next_tick.js:77:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
at Module.runMain (module.js:577:11)
at run (bootstrap_node.js:352:7)
at startup (bootstrap_node.js:144:9)
at bootstrap_node.js:467:3
但是,如果我先运行 js
文件,然后运行 C ++
/ boost> :: boost :: exception_detail :: clone_impl< boost :: exception_detail :: error_info_injector< boost ::'的实例时,调用/ p>
While, if I run the js
file first and then the C++
one, I receive:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
what(): bind: Address already in use
Aborted (core dumped)
推荐答案
您需要设置 reuseAddr:true
创建套接字:
You need to set reuseAddr: true
when creating your socket:
var server = dgram.createSocket({ type: 'udp4', reuseAddr: true });
您还需要更改在C ++端创建套接字的方式。目前,您正在使用的 udp :: socket
构造函数签名将导致套接字立即绑定。不幸的是,您需要先< 绑定之前设置 reuse_address
选项。要做到这一点,尝试这个intead:
You will also need to change how you create your socket on the C++ side. Currently the udp::socket
constructor signature you're using will cause the socket to bind immediately. Unfortunately, you need to set the reuse_address
option first before binding. To do that, try this intead:
udp::socket socket(io_service, udp::v4());
socket.set_option(boost::asio::socket_base::reuse_address(true));
socket.bind(udp::endpoint(udp::v4(), port));
这篇关于UDP绑定:地址已在使用中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!