首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >upnp、c++和boost asio

upnp、c++和boost asio
EN

Stack Overflow用户
提问于 2016-08-28 20:53:30
回答 1查看 474关注 0票数 1

我将miniupnpc与我的包装器一起使用。当我的朋友运行我的应用程序时,该应用程序会创建upnp。

但是,他仍然没有收到来自我的电脑的信息。我做错了什么?

源客户端:

代码语言:javascript
复制
upnp::upnp_wrapper upnp{};
upnp.redirect(upnp::internal_port{ 41052 }, 
              upnp::external_port{ 41052 }, 
              upnp::eProtocol::UDP, "Description");

io_service service;
ip::udp::socket socket{ service };
socket.open(ip::udp::v4());
socket.bind(ip::udp::endpoint{ ip::udp::v4(), 41052 });

ip::udp::endpoint server_ep{ ip::address::from_string("100.71.188.14"s), 9999u };

std::cout << "open: " << socket.local_endpoint().address().to_string() << ':' << socket.local_endpoint().port() << '\n';



cout << "send request\n";
socket.send_to(buffer("open: " + socket.local_endpoint().address().to_string() + ':' + std::to_string(socket.local_endpoint().port()) + '\n'), server_ep);
cout << "request sent\n\n";

array<char, 60> buff = {};
cout << "wait answer\n";
auto answer_length = socket.receive(buffer(buff));
cout << "answer recived: ";
cout.write(buff.data(), answer_length);

cout << "\nsend goodd\n";
socket.send_to(buffer("GOOOOD"s), server_ep);


cout << "\n\nEND";
cin.get();

和服务器测试源:

代码语言:javascript
复制
using namespace std;
using namespace literals;
using namespace boost::asio;

io_service service;
ip::udp::socket socket{ service };
socket.open(ip::udp::v4());
socket.bind(ip::udp::endpoint{ ip::udp::v4(), 9999u });
ip::udp::endpoint client_ep;

array<char, 100> buff = {};
cout << "start recive\n";
auto recived_length = socket.receive_from(buffer(buff), client_ep);
cout << "message recived from: " + client_ep.address().to_string() + ':' + to_string(client_ep.port()) + "\n-> ";
std::cout.write(buff.data(), recived_length);

cout << "\n\nsend answer\n";
socket.send_to(buffer("ANSWER"s), client_ep);
cout << "answer sent\n";

cout << "start good\n";
 recived_length = socket.receive_from(buffer(buff), client_ep);
std::cout.write(buff.data(), recived_length);

cout << "\nEND";
cin.get();

我用upnp和boost asio做错了什么?如何正确使用upnp打开内部和外部端口?

EN

回答 1

Stack Overflow用户

发布于 2021-10-16 18:05:35

我想是因为防火墙的缘故。也许turn server是解决方案

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39191472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档