QtNat – Open you port with Qt UPnP

https://news.ycombinator.com/rss Hits: 9
Summary

QtNat is a lightweight C++ library built with Qt 6 that simplifies NAT port mapping using UPnP (Universal Plug and Play). It is designed to help developers easily expose local services to external networks without requiring manual router configuration for users. By leveraging UPnP, QtNat automatically communicates with compatible routers to create port forwarding rules at runtime. This makes it particularly useful for peer-to-peer applications, multiplayer games, remote access tools, and any software that needs reliable inbound connectivity behind a NAT. QtNat provides a simplified API to do all steps automatically: discovery and mapping. This has been tested on my local device. Feel free to test it and improve it. Use it UpnpNat nat; QObject::connect(&nat, &UpnpNat::statusChanged, [&nat, &app]() { switch(nat.status()) { case UpnpNat::NAT_STAT::NAT_IDLE: case UpnpNat::NAT_STAT::NAT_DISCOVERY: case UpnpNat::NAT_STAT::NAT_GETDESCRIPTION: case UpnpNat::NAT_STAT::NAT_DESCRIPTION_FOUND: break; case UpnpNat::NAT_STAT::NAT_FOUND: nat.requestDescription(); break; case UpnpNat::NAT_STAT::NAT_READY: nat.addPortMapping("UpnpTest", nat.localIp(), 6664, 6664, "TCP"); break; case UpnpNat::NAT_STAT::NAT_ADD: qDebug() << "It worked!"; app.quit(); break; case UpnpNat::NAT_STAT::NAT_ERROR: qDebug() <<"Error:" <<nat.error(); app.exit(1); break; } }); nat.discovery(); We create the object (l:0) We connect to statusChanged signal to get notified (l:2) When status is NAT_FOUND, we request the description (l:11) When status is NAT_READY, we request the port mapping (l:14) When status is NAT_ADD, It means the port mapping request has been added, It worked! The application quits.(l:17) When status is NAT_ERROR, Error occured and display the error text. The application exits on error. (l:21) We connect to error changed in order to detect errors. (l:14) We start the discovery. (l:28) Technical explainations The discovery Basically, we need to know if there is a upnp server around. To do so, w...

First seen: 2026-01-09 20:52

Last seen: 2026-01-10 04:53