直接P2P连接
这个主题类似于这个线程
我对这个主题相当陌生网络编程,除了在一台本地机器上进行基本的 TCP/UDP 之外,从未做过任何事情。 现在我正在开发一个需要 P2P 网络支持的应用程序。 更具体地说,我需要应用程序通过互联网进行连接和通信,最好不使用服务器在客户端之间进行匹配。
我知道并假设几乎所有用户都位于路由器后面,这使过程变得复杂,因为两个客户端都无法初始化与另一个客户端的直接连接。
我知道 UPnP 是一个允许端口转发的选项,无需用户手动配置,但到目前为止,这不是一个选项。 他们有办法实现我的目标吗?或者我需要该服务器吗?
This topic resembles this thread
I'm rather new to the topic of network programming, never having done anything but basic TCP/UDP on a single local machine. Now I'm developing an application that will need P2P network support. More specifically I will need the application to connnect and communicate across the internet preferably without the use of a server to do the matchmaking between the clients.
I'm aware and assuming that almost all users are behind a router which complicates the process since neither clients will be able to initialize a direct connection to the other.
I know UPnP is an option to allow port forwarding without having the users configure this manually, but as of now this is not an option. Is they any way to achieve my goal or will I need that server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要一台服务器来交换 IP 地址等。 正如另一个线程字面上指出的那样,保证连接的唯一方法是通过服务器进行代理。 大多数点对点系统在大多数情况下使用UPnP和NAT Hole Punching(此方法需要中继端口信息的服务器并且仅适用于UDP)来建立连接。
NAT 打洞的工作原理是两个客户端都建立到服务器的连接,然后两者都尝试直接连接到对方中继到对方的端口。 大多数 UDP NAT 都会在短时间内记住 IP 地址和端口,因此尽管数据从未到达另一端(这对于 UDP 来说并不重要),但另一个客户端稍后会尝试连接到该报告作为 NAT期待答复。
You'll need a server to exchange IP address and such. As the other thread literally points out, the only way of guaranteeing a connection is to proxy through a server. Most peer to peer systems use UPnP and NAT Hole Punching (this method needs a server relaying port information and only works with UDP) to establish a connection in most cases.
NAT Hole Punching works by both clients establishing a connection to a server, then the both try to connect directly to a port that the other has relayed to the other. Most UDP NAT remember the IP address and port for a short time, so although the data never made it to the other end (not that this matters with UDP) the other client will try to connect a few moments later to that report as the NAT would expect the reply.
RFC 5128,“点对点状态”是一本非常好的读物,专为您而设计:-) -跨网络地址转换器 (NAT) 的对等 (P2P) 通信”。
A very good reading, made just for you :-), is RFC 5128, "State of Peer-to-Peer (P2P) Communication across Network Address Translators (NATs)".
使用 WCF 查看 P2P 通道: http://msdn.microsoft.com/ en-us/library/cc297274.aspx
它工作得很好。
Check out the P2P channel with WCF: http://msdn.microsoft.com/en-us/library/cc297274.aspx
It works quite well.
忽略 UPnP(遗憾的是,它仅适用于某些路由器)并且没有中央服务器,我不确定当两个用户都位于 NAT 之后时是否可以创建直接连接。
Ignoring UPnP (which only works with some routers, unfortunately), and no central server, I'm not sure it would be possible to create a direct connection when both users are behind a NAT.
为了避免服务器“匹配”,您可以像 Skype 那样设置一些对等点作为 NAT 后面的其他对等点的中继。 您总是需要某种引导机制,因此集中式服务器可能会以某种方式进入您的系统(当然,这取决于您的开发内容)。
Well to avoid a server to "matchmake" you could do what skype does and set up some peers as relays to others behind NATs. You wll always need some sort of boot strapping mechanism, so a centralized server will probably play into your system somehow (depending on what your developing, of course).