是否可以通过获取IP地址来使用套接字连接两个ios设备? [英] Is it possible to connect two ios devices using sockets by getting their IP addressess?

查看:115
本文介绍了是否可以通过获取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频道上交易或亲自交易,并手动输入),这可以自动化到不同程度。还有一些可以帮助的快捷方式 - 只要局域网上有一个对等方已经存在,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相互通信。知道每个设备的公共地址和内部地址的人可以安排他们建立彼此的连接。你可以让普通的同伴这样做(虽然这会使Jumpstart问题更大)或者让你的超级同伴/介绍服务器/等。做它。

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设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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