从本地网络与公共 IP 通信(WinSock、C)

发布于 2024-12-02 01:03:15 字数 364 浏览 0 评论 0原文

我是这个论坛的新手(至少在提问方面),所以请耐心等待。我正在尝试编写简单的客户端-服务器应用程序,以便使用数据报(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街道布景 2024-12-09 01:03:15

对于 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。此映射通常会在一段时间后超时。

这意味着在简单的情况下,您需要做的就是:

  1. 在客户端上,使用相同的端口发送到服务器并侦听响应;
  2. 在服务器上,使用接收数据包的同一服务器端口,在接收客户端数据包的地址和端口上响应客户端;
  3. 让客户端在会话中发送初始数据包;
  4. 不要让“连接”每次闲置超过几分钟。

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 port 65000 to the external address 64.34.119.12:6789, via a gateway with external IP address 192.0.43.10. When the gateway sees your internal client send a UDP packet from 192.168.4.5:65000 to the external address, it will NAT it to an external address and port, like 192.0.43.10:5500. Your server will see a packet with a source address of 192.0.43.10:5500 and destination address 64.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 of 64.34.119.12:6789 and a destination of 192.0.43.10:5500, it will redirect it back to 192.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:

  1. On the client, use the same port to send to the server and listen for responses;
  2. On the server, respond to the client at the address and port that the client's packet was receieved from, using the same server port that recieved the packet;
  3. Have the client send the initial packet in the conversation;
  4. Don't leave the "connection" idle for more than a few minutes at a time.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文