从本地网络与公共 IP 通信(WinSock、C)
我是这个论坛的新手(至少在提问方面),所以请耐心等待。我正在尝试编写简单的客户端-服务器应用程序,以便使用数据报(UDP)套接字与 C 和 Windows 进行通信。
该场景非常简单,我希望能够从某个本地网络(在网关/NAT 后面)中的计算机向具有公共 IP 的某个主机发送/接收数据。我可以发送数据,但似乎无法接收任何返回的数据,因为远程服务器看不到客户端的本地IP。
我不希望用户在 GATEWAY 上执行手动端口转发,而且由于安全问题我也不想使用 UPnP(尽管它也应该由用户启用)。
我认为这是可能的,因为像 Skype 或浏览器这样的应用程序可以做到这一点,但是如何做到呢? 是否有一些自动转发的端口或类似的东西?我已经厌倦了在网上搜索......请帮忙!
I'm new to this forum (at least at asking questions), so please be patient. I'm trying to write simple client-server applications to communicate using datagram (UDP) sockets, with C and Windows.
The scenario is very simple, I want to be able to send/receive data from a machine in some local network (behind a GATEWAY/NAT) to some host having public IP. I can send the data, but it seems impossible to receive any data back, because the remote server can't see the client's local IP.
I don't want the user to perform manual port forwarding on the GATEWAY, and I also don't want to use UPnP because of security issues (though it also should be enabled by the user).
I think it is possible because applications like Skype or Browsers can do it, but how ?
Are there some automatically forwarded ports or things like that ? I'm exhausted of searching the web ... PLEASE HELP !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 UDP 连接,大多数家庭 NAT 网关会自动为每个出站数据包创建临时反向映射。
考虑这样一个场景:您的客户端在 IP 地址为
192.168.4.5
的内部计算机上运行,从端口65000
向外部地址64.34.119.12 发送 UDP 数据包:6789
,通过具有外部 IP 地址192.0.43.10
的网关。当网关看到您的内部客户端从192.168.4.5:65000
向外部地址发送 UDP 数据包时,它会将其 NAT 到外部地址和端口,例如192.0.43.10:5500< /代码>。您的服务器将看到一个源地址为
192.0.43.10:5500
和目标地址为64.34.119.12:6789
的数据包。路由器还设置反向映射,因此,如果它看到源为64.34.119.12:6789
且目标为192.0.43.10:5500< 的数据包到达外部接口, /code>,它将重定向回
192.168.4.5:65000
。此映射通常会在一段时间后超时。这意味着在简单的情况下,您需要做的就是:
For UDP connections, most home NAT gateways will automatically create a temporary reverse mapping for each outbound packet.
Consider a scenario where your client is running on the internal machine with IP address
192.168.4.5
, sending a UDP packet from port65000
to the external address64.34.119.12:6789
, via a gateway with external IP address192.0.43.10
. When the gateway sees your internal client send a UDP packet from192.168.4.5:65000
to the external address, it will NAT it to an external address and port, like192.0.43.10:5500
. Your server will see a packet with a source address of192.0.43.10:5500
and destination address64.34.119.12:6789
. The router also sets up a reverse mapping, so that if it sees a packet arrive on the external interface with a source of64.34.119.12:6789
and a destination of192.0.43.10:5500
, it will redirect it back to192.168.4.5:65000
. This mapping typically times out after a short while.This means that in simple cases, all you need to do is: