xcode 是否可以通过获取 IP 地址来使用套接字连接两个 ios 设备?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10210835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to connect two ios devices using sockets by getting their IP addressess?
提问by AnishGupta
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.
我想在没有任何服务器实现的情况下通过套接字连接多个设备。我将仅使用它来获取将注册的设备的 IP 地址。
回答by abarnert
There are two major problems to peer-to-peer communications: discovery, and reachability.
点对点通信有两个主要问题:发现和可达性。
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.
首先,您需要知道其他对等方的 IP 地址才能连接到它们。一旦你连接到一个对等点网络,他们就可以互相更新网络状态,向彼此推荐更好的对等点,传递加入和离开的新对等点的通知等。但是你有设计和实施交易该信息的机制。更重要的是,你需要以某种方式快速启动,因为当一个新的对等点启动时,它只是在一个网格中,它没有信息可以提供给自己。
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.
一种可能性是拥有一些众所周知的“超级对等体”(您运行的),它们始终处于连接状态,并将它们的地址放入应用程序中。或者您可以使用“介绍服务器”而不是对等点,提供相同的功能。或者你可以有一些外部交易地址的方式(最简单的是用户在网络论坛或 IRC 频道上交易它们或亲自手动输入它们),这可以在不同程度上实现自动化。还有一些捷径可以提供帮助——只要 LAN 上的一个对等点已经存在,Bonjour 就可以让其他对等点进入网格;GameCenter/GameKit 可作为自动化的外部交易网络;等等。
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.
一旦你解决了发现问题,你仍然有可达性问题。大多数 iOS 设备通常没有可公开访问的 IP 地址;相反,它们支持进行网络地址转换的路由器,无论它们是家庭 WiFi 路由器还是蜂窝运营商的 3G 网络。这意味着您需要某种方法来进行 NAT 打孔,以使两部 iPhone 相互通话。知道每个设备的公共地址和内部地址的人可以安排它们相互建立连接。您可以让普通对等节点执行此操作(尽管这会使启动问题变得更大),也可以让您的超级对等节点/介绍服务器等。做吧。
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.
如果您想自己构建所有这些,您可能需要查看其他实现。BitTorrent(包括跟踪器和 DHT)被很好地理解和记录在一个连续的层次上,从好奇的最终用户的“谎言到孩子”到详细的协议规范和开源实现。然后看看其他一些 P2P 网络,因为 BitTorrent 并不完美,并且不会尝试做每个人都想出的所有事情。
回答by MacTeo
You can use GameKit. It has the matchmakingapi that can help you.
It can be used for non game apps.
它可用于非游戏应用程序。
回答by nflacco
I've been working on something similar and it's a giant pain in the ass. There are 3 considerations: 1) Reachability 2) Discovery 3) The connection itself.
我一直在研究类似的东西,这是一个巨大的痛苦。有 3 个考虑因素:1) 可达性 2) 发现 3) 连接本身。
1) Don't even consider using 3g/4g, it just won't work well for keeping an open socket connection.
1) 甚至不要考虑使用 3g/4g,它不能很好地保持开放的套接字连接。
2) I'd use some sort of broker service between the two on the internet to connect the two. For discovery, you can just list what devices are available on the service.
2)我会在互联网上的两者之间使用某种代理服务来连接两者。对于发现,您可以只列出服务上可用的设备。
3) For the connection, I find the IOS socket libraries to be rather painful to use, but if you go down to the BSD socket level it's not as bad. I think it'd be very interesting to use zmq sockets; that might simplify writing the broker service.
3) 对于连接,我发现 IOS 套接字库使用起来相当痛苦,但如果你深入到 BSD 套接字级别,它并没有那么糟糕。我认为使用 zmq 套接字会很有趣;这可能会简化编写代理服务。
回答by gage
You can't. If the device is all online with wifi, it maybe possible and rely on the router setting just like pc connect. If some device is connected with 3g or gprs protocal , they may have no ip address at all.
你不能。如果设备都通过 wifi 在线,则可能像 pc connect 一样依赖路由器设置。如果某些设备使用 3g 或 gprs 协议连接,则它们可能根本没有 IP 地址。