1

I am doing UDP socket programming in C. In order to listen to a port, I need to forward ports in my router. My question is how to avoid doing that and still being able to communicate over the internet, if not possible with sockets, what is the lowest level possible? In other words, every device can listen to an http server, so is http the only unlocked way to go?

2

Usually, your ISP gives you a single IP address, and your home router does network address translation (NAT) to pretend to your ISP that all the devices in your home network are just a single device with the same address as the router itself.

Because of this, if anyone wants to contact your home network from the outside, the router has to "forward a port" to the device in your home network where the service is running, because the only IP address visible from the outside is the router's IP address.

If you don't want that, the only alternative is either to run your service directly on the router, or disconnect the router from your ISP and instead connect the computer where the services run directly (if it has the hardware to do so).

There is no other way, no matter what protocol you use.

You can also pay your ISP to give your more than one IPv4 address (this will be expensive). Or, if your ISP gives you an IPv6 global prefix, then each of the devices in your home network will have its own IPv6 address, which is reachable from the outside. So there's no NAT, no port forwarding is necessary, but it will only work for IPv6.

OTOH, setting up port forwarding isn't exactly black magic, so just do it.

Edit

When you "visit a web site", i.e. your local http client will contact the remote http server, the NAT in the router will rewrite the source address of the outgoing packets from the address of your private device to the ISP's IP address. It will also remember that a local device opened an outgoing connection, and when incoming packets arrive that belong to that connection, it will conversely rewrite the destination address and send them to the local device. That's what NAT is.

So for outgoing connections from your local device, you don't need port forwarding. NAT handles this for you. For incoming connections, e.g. if you run a http server, you need to tell your router "please forward port 80 to the following local device".

How can i open the port without accesing router settings?

For incoming connections, you can't. As I already tried to explain.

Edit

If you google example programs for UDP, there'll always be a server listening on a port, and a client not listening, but contacting the server (and after that both server and client can exchange packets in any direction). So "how do you receive info without listening to a port" is that you write the client, not the server, and then the server can send data to the client, so the client "receives info".

You can't run the server behind NAT without port forwarding. Period. No matter how often you ask. Not for TCP, not for UDP, not by "using a low level protocol".

If you don't want to enable port forwarding in the GUI of the router: Many routers allow you to set port forwarding, sometimes even temporary port forwarding, via UPnP. Your router may or may not have this feature.

There are also other tricks, like first contacting a general kind of server, which then will establish a connection with some other peer behind NAT (see e.g. the STUN protocol).

But if you are behind NAT, you first have to contact a server on the "real" internet. This server will be listening on a port, your client won't. Or, if you have a server listening to a port, you need to set up port forwarding. There is no other choice. Live with it.

  • I mean how an http client listens to the server, i see the port forwarding setting of the router and browsers don't add port forwardings for every device that is browsing the web – Fuel Sep 23 '18 at 18:52
  • Which part of the explanation why browsers (http clients with outgoing connections) don't need to add port forwardings is not clear? – dirkt Sep 23 '18 at 18:55
  • How does my phone receive a website if it is not listening? – Fuel Sep 23 '18 at 18:56
  • Clients always contact the server, so it's an outgoing connection, even though the answers are incoming and get rewritten, as described above. Your phone also runs a web browser as client, not as server, so it's not "listening" in the technical sense. It receives incoming packets which are part of a connection. – dirkt Sep 23 '18 at 18:59
  • How can i receive info without listening to a port?, i want to receive packets as the browser does, is that only possible with TCP or can also be done with UDP? – Fuel Sep 23 '18 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.