编程 P2P 应用程序 [英] Programming P2P application

查看:26
本文介绍了编程 P2P 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个在端口 4900 上运行的自定义 p2p 程序.在某些情况下,当此人位于路由器后面时,无法从 Internet 访问此端口.

I am writing a custom p2p program that runs on port 4900. In some cases when the person is behind a router, this port is not accessible from the internet.

是否有自动启用从 Internet 访问端口的方法.我不太确定其他 p2p 应用程序是如何工作的.

Is there an automatic way of enabling the access to the port from the internet. I am not really sure of how other p2p applications work.

任何人都可以对此有所了解吗?

Can anyone please throw some light on this?

推荐答案

简而言之,P2P 连接.假设我们在这里谈论 UDP.以下步骤也可以通过一些调整应用于 TCP.

P2P connectivity in a nutshell. Assume we're talking about UDP here. The steps below can also be applied to TCP with some adjustments.

  1. 枚举您所有的本地 IP 地址(通常只有 1 个).为每个具有 IP 地址的适配器在给定端口号**上创建一个 UDP 套接字.

  1. Enumerate all your local IP addresses (usually only 1). Create a UDP socket on a given port number** for each adapter with an IP address.

对于在步骤 1 中创建的每个套接字,联系具有相同套接字的 STUN 或 TURN 服务器以发现您的外部 IP 地址并发现内部端口号映射到 NAT 外部的内容(它并不总是相同的)端口值).也就是说,你的本地地址 192.168.1.2:4900 对外界来说可能是 128.11.12.13:8888.当使用相同的本地端口到其他 IP 地址时,一些 NAT 并不总是使用相同的端口映射.TURN 还会为您提供一个中继地址".如果路由器支持该协议,您还可以使用 UPNP 直接从路由器获取端口映射地址.

For each socket created in step 1, contact a STUN or TURN server with that same socket to discover your external IP address and to discover what the internal port number maps to outside of the NAT (it's not always the same port value). That is, your local address 192.168.1.2:4900 might be 128.11.12.13:8888 to the outside world. And some NATs don't always use the same port mapping when using the same local port to other IP addresses. TURN will also provide you a "relay address". You can also use UPNP to get a port mapped address directly from your router, if it supports that protocol.

通过集合服务(SIP、XMPP、即时消息、网络服务、电子邮件、带字符串的杯子),将您的候选地址列表发布到服务或向其他客户端发送通知,说嘿,我想和你联系".此消息包括在步骤 1 和 2 中收集的所有候选地址"(IP 和端口对).

Through a rendezvous service (SIP, XMPP, instant message, web service, email, cups with strings), publish your address candidate list to a service or send a notification to the other client that says, "hey, I want to connect with you". This message includes all the "address candidates" (ip and port pairs) collected in steps 1 and 2.

远程客户端在收到连接邀请后,也会执行上述第 1 步和第 2 步.然后通过他收到邀请者候选人名单的同一渠道发回他的候选人名单.

The remote client, upon receiving the invite to connect, performs step 1 and 2 above as well. Then sends back his candidate list through the same channel that he received the inviter's candidate list on.

打孔步骤.两个客户端都开始通过 UDP 向对方的候选地址发送测试消息,并在他们的一端侦听相同的消息.每当收到消息时,回复它来自的地址.最终,客户端会发现他们有一对地址,他们也可以可靠地发送数据报.通常,一个端点会最终决定与哪个地址对(套接字)进行通信,并且协议有助于该端点将这一决定告知另一个端点.

Hole punching step. Both clients, start sending test messages over UDP to the other side's address candidates and listening for the same messages on their end. Whenever a messages is received, reply back to the address from which it came. Eventually, the clients will discover that they have a pair of addresses that they can reliably send datagrams too. Typically, one endpoint makes the final decision on which address pair (sockets) to communicate with and the protocol facilitates this endpoint telling the other endpoint this decision.

**- 通常最好不要依赖众所周知的 P2P 客户端端口.因为位于同一 NAT 或防火墙后面的两个客户端不太可能同时使用您的软件.

**- usually best to not to rely on a well known port for P2P clients. Because two clients behind the same NAT or firewall would not likely be able to use your software at the same time.

这里简要总结了一些需要探索的技术.

Here is a quick summary of some technologies to explore.

STUN - 是一个简单的服务器和协议,供 NAT/路由背后的客户端发现他们的外部 IP 和端口映射是.

STUN - Is a simple server and protocol for clients behind a NAT/route to discover what their external IP and port mappings are.

TURN 是 STUN 的扩展,但支持在防火墙和 NAT 阻止直接连接的 P2P 连接场景中进行中继.

TURN is an expansion to STUN, but supports relaying for P2P connectivity scenarios where firewalls and NATs prevent direct connections.

ICE 是 STUN 和 TURN 用于建立 P2P 连接的一组步骤.ICE 是上述步骤 1-5 的正式协议.这里这里.

ICE is a set of steps by which STUN and TURN are used for setting up a P2P connection. ICE is a formal protocol for steps 1-5 above. Two excellent set of slides on ICE are here and here.

WebRTC 是 ICE 标准的变体,也是使用 STUN 和 TURN 进行 P2P 会话的参考库.

WebRTC is a variant of the ICE standard as well as a reference library for make P2P sessions with STUN and TURN.

UPNP + Internet 网关设备协议 - 一些路由器支持主机自动获取端口映射.

UPNP + Internet Gateway Device Protocol - Some routers support this for hosts to automatically obtain port mappings.

libnice 是一个实现 ICE 的 Linux 开源 C 库(可能适用于 Windows).

libnice is an open source C library for Linux (and might work on windows) that implements ICE.

libjingle 是 Google 的另一个 ICE 实现(C++).适用于 Windows 和 Linux.

libjingle is another ICE implementation (in C++) from Google. For Windows and Linux.

PJNATHPJSIP 编码库套件.它是 ICE 堆栈(C 代码)的良好实现,并已移植到许多平台.(Windows、Linux、Mac、iOS、Symbian 以及即将推出的 Android).

PJNATH is a library within the PJSIP suite of coding libraries. It is a good implementation of an ICE stack (C code) and has been ported to a lot of platforms. (Windows, Linux, Mac, iOS, Symbian, and soon Android).

最后,我有一个公然的插件供您使用我的 STUN 服务器代码库.

And finally, I have a blatant plug for you to use my STUN server code base.

这篇关于编程 P2P 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆