iPhone - 通过互联网的点对点连接

发布于 2024-12-20 23:59:49 字数 242 浏览 5 评论 0原文

我正在构建一个使用异步套接字与 Web 服务器连接的应用程序。 为了发送大量数据,我想使用异步套接字将两部 iPhone 连接到点对点连接。我对 GCDAsyncSocket 比对 iOS SDK 套接字 API 更熟悉。

我已经做了类似的事情,但只有在本地网络中,我才会使用 NSNetService 来发布存在并从 p2p 连接获取所需的地址。

您能给我一些关于如何使用 p2p 套接字连接通过互联网连接两部 iPhone 的提示吗?

I am building an app that uses a async socket connection with a web server.
For sending large amounts of data I would like to connect two iPhones into a Peer to Peer connection using an async socket. I am more familiar with GCDAsyncSocket than to the iOS SDK socket API.

I have done something similar, but only within a local network were I would user NSNetService to publish a presence and get the addresses needed from the p2p connection.

Can you give me a hint on how to connect two iPhone over the internet using a p2p socket connection?

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

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

发布评论

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

评论(2

情何以堪。 2024-12-27 23:59:49

我对 NSNetService 不太了解,但它似乎只适用于本地网络。如果您想建立从一台 iphone 到另一台 iphone 的 p2p 连接,您的路由中总会遇到多个 NAT(网络地址转换器)的问题。如何找到到对端的地址和路由?以及如何通过所有的防火墙和 NAT?

我正在开发一个需要处理相同问题的应用程序,并且我已经回答了一个类似的问题,在此之前我将在此引用:

事实上,这是可能的。你可能想用谷歌搜索一个叫做
“UDP 打洞”或“TCP 打洞”。

简而言之,主要方法:假设你有一个类似继电器的东西
服务器,即互联网上公开的某个服务器
可从连接到 www 的每个专用 LAN 进行寻址。不
您的两个客户端 A 和 B 位于(不同的)专用 LAN 中,
正在进行一些网络地址转换(NAT),想要
建立点对点连接。

首先两者都会告诉服务器它们的IP地址和端口
他们有自己的局域网。在UDP或TCP数据包中,服务器将
查找设备(或 NAT(路由器))的公共地址和端口。
所以服务器知道私有和公共IP地址以及
端口。

如果现在A想要与B通信,它会向服务器请求帮助。这
服务器将向 B 发送一条消息,表示 A 希望与她通信
告诉她A的公私IP和端口。 A 夺回 B 的公众
以及私人信息和端口。

现在这就是奇迹发生的地方。两个客户端现在都发送数据包
同时建立与私人和公共的连接
对方的地址,从而在他们的 NAT 中打入一个整体
这样传入的连接就不会被阻止。即使一个
一方的连接建立数据包将在整个过程之前到达
创建后,其他的数据包将通过,以便
可以创建连接。

谨防某些扫描 IP 地址数据并进行转换的 NAT
它们也是如此,但是如果您加密数据或更改数据的外观
地址(补充,...)你会没事的。

现在主要问题是,服务器如何与其中之一通信
没有活动连接的客户端。那么在这种情况下你可以
使用“连接反转”和苹果的“推送通知”。使用
“推送通知”(pn) 告诉 NAT 后面的客户端有
发生了一些有趣的事情,它应该联系服务器。
一旦完成,连接就处于活动状态并且可以在
之前描述过时尚。

我希望这可以帮助一些遇到这个问题的人,尽管
帖子很旧了!

现在,如果您发现此解决方案有帮助并尝试实现它,请告诉我您是否成功获取服务器端服务器-客户端连接的公共 IP 和端口。您还可以回答我在 stackoverflow 上提出的问题

I do not know much about NSNetService but it seems that it only works in a local network. If you want to establish a p2p connection from an iphone to another you will always have the problem of several NATs (Network address translators) in your route. How to find the address and route to the peer? And how to get trough all the firewalls and NATs?

I am working on an app that needs to deal with the same problem and I have answered a similar question before which I will cite here:

Actually, it IS possible. You may want to google for something called
"UDP hole punching" or "TCP hole punching".

The main approach in short: Assuming you got something like a relay
server, that is some server in the internet that is publicly
addressable from every private LAN that is connected to the www. No
you have your two clients A and B in (different) private LANs, with
some Network address translation (NAT) going on, that want to
establish a peer to peer connection.

First of all both will tell the server their IP address and the port
they have in their own LAN. In the UDP or TCP packet, the server will
find the public address and port of the device (or the NAT (router)).
So the server knows the private and the public IP address as well as
the ports.

If now A wants to communicate with B, it asks the server for help. The
server will send a message to B that A wants to communicate with her
telling her A's public and private IP and port. A gets back B's public
and private information and port.

Now here is where the magic happens. Both clients now send packets out
to establish a connection simultaneously to the private and public
addresses of the other party and thus punching a whole in their NATs
such that incoming connections will not be blocked. Even if one
party's connection establishing packets will arrive before this whole
is created, the other's packets will get through to such that a
connection can be created.

Beware of some NATs that scan the data for IP addresses and translate
them as well, but if you encrypt your data or change the appearance of
the address (complement, ...) you will be fine.

Now the master question, how can the server communicate with one of
the clients without an active connection. Well in this case you can
use "connection reversal" and apple's "push notifications". Use the
"push notifications" (pn) to tell a client behind a NAT that there is
something of interest going on and that it should contact the server.
Once it has done that the connection is active and can be used in the
previous described fashion.

I hope this helps some people that get to this problem although the
post is quite old!

Now if you find this solution helpful and you try to implement it, please tell me if you are successful in getting the public ip and port of the server-client connection on the server side. You can also answer my question about this on stackoverflow.

白首有我共你 2024-12-27 23:59:49

请具体说明,您可以使用 GCDAsyncSocket 连接 2 部手机,您可以使用来自 Github 的示例代码。

Please be specific, u may use GCDAsyncSocket to connect 2 phones, u may use sample code from Github.

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