SirDan
Posts: 6
Joined: Sat Sep 07, 2013 5:46 am

Pi to Computer

Sat Sep 07, 2013 5:53 am

I recently tried learning networking and with it sockets. Upon attempting to make simple connections I came to realize that only through local networks can I ever get them to connect. I understand now that it's only with port forwarding that I can get these simple connections to work, however I am now confused as to how every other application I use seems to do this without an issue.

I don't understand how, if every socket connection I make requires port forwarding to get past the router/firewall, can any socket connection be established back and forth externally without that always being done? How can an external connection specify a device without the port forwarding? Can I implement this with sockets?

Lastly, am I going about learning network programming the wrong way by tackling sockets only right now?

Thank you! :)
Last edited by SirDan on Sun Sep 08, 2013 9:55 pm, edited 1 time in total.

User avatar
topguy
Posts: 5624
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Pi to Computer

Sun Sep 08, 2013 2:10 pm

Your internet router does something called Network Address Translation (or NAT'ing for short).

When any PC on your LAN connects to any internet site/socket, the router remove the LAN ip-address and inserts the routers external address instead. It also remembers which LAN address sent this and when the external server answers it does the opposite change before forwarding the packet to the correct PC.

http://en.wikipedia.org/wiki/NAT

This works for any connection that is initiated from your LAN, when an external client tries to connect with a server on your LAN there is no entry in the NAT table for this combination of ingoing and outgoing addresses so then you need to make a static NAT rule and thats we normally call "port-forwarding".

Edit:
If your router supports "UPnP" there is the possibility to tell the router that you want a port forwarded to you via the LAN without going through the web-gui.

User avatar
MrBunsy
Posts: 185
Joined: Mon Feb 20, 2012 1:48 pm
Location: Southampton, UK
Contact: Website

Re: Pi to Computer

Sun Sep 08, 2013 3:34 pm

I'm pretty sure a few modern games use this technique: http://www.raknet.net/raknet/manual/nat ... rough.html to allow people without port-forwarding set up manually or with UPnP to host. But for the vast majority of things, if you want to host, rather than simply connect to, something then you do indeed need to port-forward.

Return to “General programming discussion”