C ++套接字:通过Internet在PC之间进行通信 [英] C++ sockets: communication between PCs over internet

查看:99
本文介绍了C ++套接字:通过Internet在PC之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Winsock在Windows上编写程序,该程序可以将消息发送到另一台计算机.客户端与另一台计算机上的服务器连接并开始交换数据.

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.

在使用本地地址(192.168.1.*)的本地网络上它可以正常工作,但是我无法与公共地址(216.185.45.129)通信;甚至不是我自己的.无论我使用什么端口(未保留的端口),我都可以使用端口80成功连接到网站,但不能使用其公用IP地址连接到我的笔记本电脑在家里.

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?

-然后像Teamviewer这样的其他程序如何连接到网络上的其他计算机?

-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?

-至少我可以转发 my 路由器上的端口,但可以让客户端执行任何操作吗?或者让我的程序自动转发端口.

-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. 港口转运

您可以强制大多数路由器不映射特殊端口,而是将它们绑定到唯一的MAC地址.您可以使用UPNP来配置大多数路由器,但是出于安全原因,我不建议这样做,并且在许多路由器不允许UPNP操作的环境中,它也不起作用. 大多数路由器出于游戏原因具有端口转发功能(大多数用于P2P网络) 它适用于TCP和UDP.

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遍历

常见的方法是NAT遍历,也称为NAT打孔.我将用UDP来简短地描述它.您可以在此处上找到有关TCP和

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. 两个客户端都与服务器联系.服务器现在知道两个客户端的IP和PORT.
  2. 服务器将信息发送回客户端.
  3. 两个(!)客户端现在都在已知地址上相互发送软件包.

两个客户端都必须发送UDP程序包,并且必须接受第一个程序包丢失,这是必要的.原因是路由器.如果客户端之前已将软件包发送到源,则大多数路由器仅接受来自已映射PORT上的源的软件包.

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.

更新 关于Remy Lebau的评论,我将防火墙穿孔部分更改为NAT Traversal,这是部分错误的.

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

这篇关于C ++套接字:通过Internet在PC之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆