C# 对等网络 - 绕过路由器

发布于 2024-08-30 09:02:25 字数 198 浏览 3 评论 0原文

我想用 C# 编写一个点对点聊天程序。我正在尝试弄清楚网络是如何工作的。我知道这个概念是每个对等点同时是客户端和服务器。它可以连接并被连接。但我的问题是,如何连接到路由器后面的计算机而不将端口转发到该计算机?我知道像 Bittorrent 这样的东西可以毫无问题地做到这一点。我计划使用跟踪器来保存对等点列表以及它们侦听的端口,但我仍然不明白如何解决路由器问题。有人可以解释一下吗?

I want to code a peer-to-peer like chat program in C#. I am trying to figure out how the networking would work. I know that the concept is that each peer is a client and a server at the same time. It can connect and be connected to. But my question is, how do you connect to a computer behind a router without the port being forwarded to that computer? I know things like Bittorrent do this with no problem. I was planning on using a tracker to keep a list of peers and the ports they listen on, but I still don't understand how to get through the router issue. Could someone please explain?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

我做我的改变 2024-09-06 09:02:25

你是对的,这不是小事。问题在于所谓的 NAT,或网络地址转换。本质上,您在自己的本地网络中拥有的 IP 是唯一的,但在公共互联网的更广泛环境中,同一路由器后面的许多人将拥有相同的公共 IP。因此,给定 IP 和一些数据,您不一定能知道另一端的哪个人应该获取它。

最有效和最简单的解决方案是让第三方通过将其自身插入数据流来调解连接。聊天中的每个人都不是同时充当“客户端”和“服务器”,而是只有一台服务器,并且位于众所周知的公共位置;然后每个人都连接到它。然后,服务器管理通信并在人们聊天时向他们发送消息。

不过,还有其他解决方案。例如,您可以使用众所周知的端口,而不是使用众所周知的服务器:所有通信都在此端口上进行。那么您不需要服务器,但所有端点都需要打开该端口,这需要事先配置。

You're right, it's not trivial. The problem is something called NAT, or network address translation. Essentially, the IP you have within your own local network is unique, but within the broader context of the public Internet, many people behind the same router will have the same public IP. Thus, given an IP and some data, you can't necessarily tell which person on the other end should be getting it.

The most effective and easiest solution is to have a third party mediate the connection by interposing itself into the data stream. Instead of each person in the chat being a "client" and a "server" at the same time, there's only one server, and it's at a well-known public location; then everybody connects to it. The server then manages the communications and sends messages to people as they chat.

There are other solutions, though. For instance, instead of using a well-known server, you could use a well-known port(s): all communications take place on this port(s). Then you don't need a server, but all endpoints need to have that port open, which requires configuration beforehand.

千笙结 2024-09-06 09:02:25

除了 John Feminella 的回答之外,您还可以考虑使用许多消费者网络设备支持的 UPNP 服务。

In addition to John Feminella's answer, you might consider using the UPNP service which many consumer network devices support.

叶落知秋 2024-09-06 09:02:25

我假设您的意思是在 NAT 环境中,如果是这样,请阅读并理解 Peer-to - 跨网络地址转换器的对等通信(Ford、Srisuresh 和 Kegel

关键概念是每个客户端必须启动一个连接,该连接将打开一条通过 NAT 设备的路径。参考Pidgin 源代码 以获得一些想法。

I'm assuming that you mean in a NAT environment, and if so read and understand Peer-to-Peer Communication Across Network Address Translators (Ford, Srisuresh & Kegel.

The key concept is that each client must initiate a connection which will open a path through the NAT device. It may be useful to refer to the Pidgin source code to get some ideas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文