简体   繁体   中英

C++ sockets: communication between PCs over internet

I'm writing a program on Windows using winsocks that can send messages to another computer. The client connects with the server in the other computer and begin exchanging data.

It works fine on my local network using local addresses(192.168.1.*), but I can't communicate with public addresses (216.185.45.129); not even my own. I can successfully connect to a website on port 80, but not to my laptop at home using its public IP address, regardless of what ports I use (unreserved ports).

So I did research online and the only solution that seems to work is port forwarding.

-But is there absolutely no other way to achieve this?

-How do other programs like Teamviewer connect to other computers on the network then?

-Is there an already open but typically unused port that I can use?

-At the very least, can I forward the ports on my router but not have the client do anything? Or maybe have my program forward the ports automatically.

The main problem is, that every router is using NAT to distinguish different computer in your lokal network against the WAN. He need to do this, because you got only one IP in the internet, but several devices in your home. To archive this, he uses groups of ports. That means, if you use to send maybe from port 2048 to a webserver in internet with two devices, the router gives one device another port (like 2049). The response has the Port of the requester, so the router can map it back. Unfortunately most router always map ports so you never now which port you have from the internet side.

There are two common ways to work around and archive your goal.

  1. Port Fowarding

You can force most router not to map special ports but bind them to unique MAC addresses. You can use UPNP to config most router to do that, but I do not recommend that for security reasons and also it does not work in many enviroments where Router do not allow UPNP manipulation. Most router have port forwarding abilities for gaming reasons (mostly it is used in P2P networks) It works with TCP and UDP.

  1. NAT Traversal

The common way is NAT traversal, also known as NAT hole punching. I will describe it in short for UDP. You can find a wiki explanation here for TCP and for UDP here. Unfortunately you need a server in the internet both clients can reach. Here the steps:

  1. Both clients contact the server. The server now know IP and PORT of both clients.
  2. Server send back the information to the clients.
  3. Both(!) clients send now packages to each other on the known address.

It is necessary that both client send a UDP package and have to accept that the first package get lost. The reason is the router. Most router only accept packages from a source on a mapped PORT if a client has send a package to that source before.

UPDATE Regarding to a comment of Remy Lebau I changed the Firewall piercing part to NAT Traversal as it was partly wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM