16

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.

freelancer
  • 1,658
  • 1
  • 16
  • 37
AnishGupta
  • 204
  • 1
  • 2
  • 8
  • 1
    Perhaps Bonjour service is what you are looking for? – user523234 Apr 18 '12 at 14:49
  • No,Devices to be connected will not be on the same network.They will be connected by their three g networks or the gprs connection of the network provider – AnishGupta Apr 19 '12 at 10:44
  • 3
    You will need some discovery mechanism. If the devices are not on the same network, you will need some sort of server. If the devices are on 3G you cannot even be sure that their IP will be unique. – starbugs Apr 27 '12 at 12:34

4 Answers4

9

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.

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.

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.

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.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • 1
    See http://stackoverflow.com/questions/6421742/tcp-hole-punching-on-iphone for more info about hole punching on iOS. See http://www.rahul.net/dholmes/ctorrent/ for the source to Enhanced cTorrent, a BitTorrent client for iPhone. – abarnert Apr 30 '12 at 17:55
  • 1
    There's an additional problem if you want long-term persistent connections: iPhones change addresses all the time. So you need to detect that from one side or the other, and possibly re-punch the hole to re-establish the connection. There are some P2P protocols that take this into account (I believe Adobe's RTMFP does, but I don't know if the old whitepapers that explained how it worked are still available), but most don't, because they were designed for desktop systems. – abarnert May 03 '12 at 20:35
5

You can use GameKit. It has the matchmaking api that can help you.

It can be used for non game apps.

MacTeo
  • 2,656
  • 1
  • 20
  • 23
  • is matchmaking api working over the internet? because i want some that kind of mechanism which can connect the multiple devices over the internet.as per my knowledge Gamekit is only able to connect the devices by bluetooth or local area network. – freelancer Apr 28 '12 at 04:56
  • 3
    You're confusing two things. GKSession, the peer-to-peer part of GameKit, only works over bluetooth or local network. GKMatchmaker does work over the internet, but it's not peer-to-peer: everything is routed through Apple's servers or your own. With a custom server and a bit of work, it can be used as part of an introducer for a real P2P mesh, but by itself it doesn't come anywhere near solving the problem. – abarnert Apr 30 '12 at 17:47
3

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.

1) Don't even consider using 3g/4g, it just won't work well for keeping an open socket connection.

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.

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.

nflacco
  • 4,972
  • 8
  • 45
  • 78
  • 1
    #1 is a good point. You have to have some application-level logic to propagate address changes (and re-punch holes) for peers when they change address. This is doable, but it does mean that if you want reliable, ordered, long-term connections, you're going to have to build them yourself at the application level, rather than relying on the magic of TCP. – abarnert May 09 '12 at 20:52
  • Oh, and it also means you need some kind of unique peer ID that's persistent at least within a session to use as an address, so you can ask the mesh for the new address of the guy you were just talking to. – abarnert May 09 '12 at 20:53
0

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.

gage
  • 109
  • 5
  • Not true. GPRS and all other mobile technologies used by iOS devices guarantee an IP address. (GPRS used to allow X.25-only, but only in the obsolete first version.) The IP address may not be publicly routable, but the same thing is true for most WiFi networks, and the same NAT punching techniques work. – abarnert Apr 30 '12 at 17:42