使用 C# 在 NAT 后面进行点对点
我有一个一般性问题,可能已在此处得到解答,但使用搜索效率不高。所以,我会再问一下,如果有相关线索,请指出我那里。谢谢。
所以,总体思路是: 我们有两个用 .NET 编写的客户端,它们想要相互通信,而不需要通过服务器路由整个流量。更重要的是,两个客户端不必都基于 .NET,但无论如何都有一个客户端是基于 .NET 的(另一个客户端可以是 Unix 上的 Objective-C、C++ 等)。
这个想法是:
- 第一个客户端建立到某个服务器的连接,以便 NAT 记住客户端位于地址 10.0.0.1:1234 上,服务器位于地址 70.0.0.7:4567 上。因此,当 70.0.0.7 将数据包发送回 NAT 时,NAT 将知道如何将其伪装给客户端。
- 第二个客户端建立到同一服务器的连接,因此第二个 NAT 也会记住地址 192.168.0.1:19234 上的内部客户端已连接到地址 70.0.0.7:7654 上的服务器,并且还能够接受返回 192.168.0.1 的数据包:19234
- 现在我们需要从 10.0.0.1:1234 建立连接192.168.0.1:19234。
两个 C# 客户端都可以了解彼此的端点(假设服务器保留 NAT 地址,它们是:Client1 - 111.0.0.1:1111 和 Client2 - 222.0.0.2:2222)。 必要的是 - Client1 能够直接将数据包发送到 222.0.0.2:2222 而无需调用 Socket.Connect 拳头,并且 Client2 也能够将数据包发送到 111.0.0.1:1111 而无需调用 Socket.Connect。
是否可以仅使用 .NET 来完成此操作,而无需使用任何特殊库,仅使用标准套接字等?
I have a general question that was probably answered somewhere here, but using search was not very productive. So, I will reask and if there's a thread about that, please point me there. Thanks.
So, the general idea is:
We have two .NET-written clients that want to talk to each other without routing the whole traffic through the server. Even more, it is not necessary for both clients to be .NET based, but one will be in any case (another can be Objective-C, C++ on Unix etc.).
The idea is:
- The first client establishes the connection to some server so NAT remembers that the client is on address 10.0.0.1:1234 and the server is on address 70.0.0.7:4567. So, when the 70.0.0.7 will send the packet back to NAT, the NAT will know how to masquerade it to client.
- The second client establishes the connection to the same server so second NAT also remembers that the internal client on address 192.168.0.1:19234 is connected to server on address 70.0.0.7:7654 and will also be able to accept packets back for 192.168.0.1:19234
- Now we need to make the connection from 10.0.0.1:1234 to 192.168.0.1:19234.
Both c# clients can know about endpoints of each other (let's say the server keeps the NATted addresses and they are: Client1 - 111.0.0.1:1111 and the Client2 - 222.0.0.2:2222).
What is necessary is - for Client1 to be able to send the packets directly to 222.0.0.2:2222 without calling Socket.Connect fist and for Client2 send packets to 111.0.0.1:1111 without calling Socket.Connect as well.
Is that possible to do with a .NET only without using any special libraries just using standard sockets etc.?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来你想做一些类似于 UDP 打洞 的事情
Sounds like you want to do something similar to UDP hole punching
或者NAT穿越
Or NAT traversal