Is it possible to connect two ios devices using sockets by getting their IP addressess?(是否可以通过获取它们的 IP 地址来使用套接字连接两个 ios 设备?)
问题描述
我想通过套接字连接多个设备,而不需要任何服务器实现.我将只使用它来获取将注册的设备的 IP 地址.
I want to connect multiple devices through socket without any server implementation.I will use that only for getting the IP addresses of the devices that will register.
推荐答案
点对点通信存在两个主要问题:发现和可达性.
There are two major problems to peer-to-peer communications: discovery, and reachability.
首先,您需要知道其他对等方的 IP 地址才能连接到它们.一旦你连接到对等点的网格,它们都可以互相更新网络状态,相互推荐更好的对等点,传递新的对等点加入和离开的通知等.但是你有设计和实施交易该信息的机制.更重要的是,您需要以某种方式快速启动事物,因为当一个新的对等点启动时,它只是在自己的网格中,并且它没有信息可以提供给自己.
First, you need to know the IP address of the other peers to connect to them. Once you're connected to a mesh of peers, they can all keep each other updated on the state of the network, suggesting better peers to each other, passing around notifications of new peers who've joined and left, etc. But you have to design and implement a mechanism for trading that information. More importantly, you need to jumpstart things in some way, because when a new peer starts up, it's in a mesh of just itself, and it has no information to give itself.
一种可能性是让少数著名的超级同行"(您运行的)始终保持连接,并将它们的地址烘焙到应用程序中.或者您可以使用介绍服务器"而不是对等服务器,提供大致相同的功能.或者您可以有一些外部交易地址的方式(最简单的是用户在网络论坛或 IRC 频道或亲自交易它们并手动输入它们),这可以在不同程度上实现自动化.还有一些捷径可以提供帮助——只要 LAN 上的一个对等点已经存在,Bonjour 就可以让其他对等点加入网格;GameCenter/GameKit 可用作自动化的外部交易网络;等等
One possibility is to have a handful of well-known "superpeers" (that you run) that are always connected, and bake their addresses into the app. Or you can have "introduction servers" instead of peers, serving much the same function. Or you can have some external way of trading addresses (the simplest is that users trade them on a web forum or an IRC channel or in person and type them in manually), which can be automated to various degrees. There are also shortcuts that can help—Bonjour can get other peers onto the mesh as long as one peer on the LAN is already there; GameCenter/GameKit can be used as an automated external trading network; etc.
一旦您解决了发现问题,您仍然会遇到可达性问题.大多数 iOS 设备通常没有可公开访问的 IP 地址;相反,它们位于进行网络地址转换的路由器之后,无论它们是家庭 WiFi 路由器还是蜂窝运营商的 3G 网络.这意味着您需要某种方式来进行 NAT 打孔,以使两部 iPhone 相互通话.知道每个设备的公共地址和内部地址的人可以安排他们建立彼此的连接.您可以让普通同行执行此操作(尽管这会使启动问题变得更大)或让您的超级同行/介绍服务器/等.去做吧.
Once you've solved the discovery problem, you still have the reachability problem. Most iOS devices usually don't have publicly-accessible IP addresses; instead, they're behind routers that do Network Address Translation, whether they be a home WiFi router or a cell carrier's 3G network. This means you need some way to do NAT Hole Punching to get two iPhones talking to each other. Somebody who knows both the public address and the internal address of each device can arrange for them to set up a connection to each other. You can have either normal peers do this (although that makes the jumpstart problem even bigger) or have your superpeers/introduction servers/etc. do it.
如果您想自己构建所有这些,您可能想查看其他实现.BitTorrent(包括跟踪器和 DHT)在从好奇的最终用户的对孩子撒谎"到详细的协议规范和开源实现的各个级别都得到了很好的理解和记录.然后看看其他一些 P2P 网络,因为 BitTorrent 并不完美,并且不会尝试做每个人都想出的所有事情.
If you want to build all of this yourself, you probably want to look at other implementations. BitTorrent (including trackers and DHT) is well-understood and documented at a continuum of levels ranging from "lies-to-children" for curious end users to detailed protocol specs and open source implementations. And then look at some other P2P networks, because BitTorrent is not perfect, and doesn't try to do everything that everyone's come up with.
这篇关于是否可以通过获取它们的 IP 地址来使用套接字连接两个 ios 设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否可以通过获取它们的 IP 地址来使用套接字连接两个 ios 设备?
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01