Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

(Not sure if this should be on the unix stackexchange site...)

When I make a socket I use IPv4 and a port number. So if I know both of these, but there's a router set up (on a home network; just learning about sockets, and experimenting), how would I access a client on that LAN? So say I'm at a friend's on their computer, and I want to connect to a socket on the computer at my house that's on my LAN, if I use my public IP address as the socket host, how would it know to go to my computer? Do I need to set up port forwarding or something on the router? (using C and Python, if that matters)

share|improve this question

migrated from Mar 14 '12 at 5:15

This question came from our site for professional and enthusiast programmers.

Forget about C/sockets. Go grab a [TCP/]IP reference book and start having a good read :) – pst Mar 14 '12 at 5:10
aww superuser? how insulting...haha. @pst it's for school – Marty Mar 14 '12 at 5:18
up vote 1 down vote accepted

Yes, the router needs to know to forward incoming packets to your computer. This is commonly done in one of the following ways:

  • Demilitarized Zone: the router forwards all incoming traffic to a designated host
  • Static port forwarding, where particular outward-facing ports are always routed to specific hosts by rules set up on the router
  • Dynamic port forwarding where patterns of inbound or outbound traffic trigger routing of particular outward-facing ports to specific hosts (e.g. if a machine makes a connection to a particular Internet host on a given port, forward a certain inbound port range to that machine)
  • Universal Plug & Play or NAT-PMP, where a machine on your network automatically sets up the forwarding for services it is hosting (commonly used by e.g. BitTorrent clients)
share|improve this answer

An "Easy mode" solution would be to set up ipv6 for all the involved computers. If the routers/ISPs don't/won't support it, you can use SixXs (or similar) to tunnel ipv6 connectivity to your boxes.

Then, using ipv6 addresses, things should just work.

share|improve this answer
interesting, i'll have to look into that. – Marty Mar 14 '12 at 5:27

On your end, the router uses NAT so it knows which external ports correspond to which internal clients.

On the other end, some sort of port forwarding is required.

share|improve this answer

I'm speaking here about NAT routers which are the majority of home routers.

If you are connecting to a port on another computer, it should go through the router just fine unless the router has blocked that port.

If you are expecting another computer to connect to yours, you must make special arrangements in the router to forward a specific port to your PC.

The choice of language doesn't make a bit of difference.

share|improve this answer

Routers' main purpose is to do internetwork layer (layer 2/3) routing (Internet Protocol v4 and/or v6 and ICMP mostly). Sockets are to do with the transport layer (layer 3/4) and so although the router may be able to interact with ports (port forwarding being one example) it mostly doesn't care (firewalling ports on the router would be another example of where it would, but that is often done with an entirely different box called a firewall).

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .