views:

635

answers:

7

Is it possible to connect a socket between two mobile devices (iPhones) over the internet?

I am trying to discover the IP of each device and connect directly. I know it can be done with Bonjour but that only works on local networks. I need a high speed connection between two devices over the internet.

Thanks.

+2  A: 

Only a suggestion, but if you can get each of them to connect to a third site, you'll then know the address of each. Send the address of phone A to phone B (and/or phone B to phone A) and then have them connect as you see fit.

KevinDTimm
+5  A: 

If you had both the IP's and they were both routable, it should be possible. I'm not sure though if the IP's given out by the 3G network are routable. If you are using WIFI, the IP you get is almost certainly not routable. Assuming you have a routable IP though, it should be possible.

The trick is just discovering the IP of the other device. The best way to do that would probably be to have each device register its IP with a web service when your app starts up, then query that service to find the IPs of other devices.

It might get a bit tricky managing "unregistering" IPs when the app is closed or when an IP changes though, but it should be doable.

ETA:

If you have a server that they can both connect to, you should be able to implement a solution using that server (i.e. without the phones actually directly connecting to each other). This would avoid the issue of having routable IPs for the phones.

As for how to bridge the sockets together - your server program would basicly work like an old-school phone switchboard operator. You'd have some kind of registration & discovery protocol built into the server software that would allow a phone to register itself with the server, query a list of the other phones currently connected to the server, indicate which phone it wants to talk to, and accept connections from other phones.

Once it knows who wants to talk to whom, your server software would connect the two together by reading from one phone's socket and writing that data to the other phone's socket.

Eric Petroelje
I can defiantly handle registering the IP of each device on a web server. However, i am concerned with what you said about Wi-Fi and 3G possibly/probably not being routable. I would like for it to work with the iPod touch also which could ONLY connect over Wi-Fi. I can set up a web server. I am guessing each device can connect to the server with a (persistant?) socket. I just do not know how to pass data between the two connections/sockets on the server. Also, it is concerning that this will be fast enough?
Jamey McElveen
Also Thanks for your feedback I feel like the water is getting less muddy.
Jamey McElveen
OK, edited my response with more info. As for performance, it would depend largely on how fast your server is and how many simultaneous connections it would have to handle. With a well-implemented server, it should be almost as fast as a direct phone-to-phone connection.
Eric Petroelje
Awesome Thanks! I will try that approach. It is a little more server intensive that I was hoping but it is coming into focus as the best solution.
Jamey McElveen
+1  A: 

Have you looked through all the 3.0 features? Specifically peer networking (the existence of which is public).

Kendall Helmstetter Gelner
peer as of 3.0 is WAN and Bluetooth. I got those working I just want to add the 3rd tier
Jamey McElveen
+1  A: 

WiFi routers often have a feature called "Port Triggering" which allows internal hosts (in this case an iPhone) to inform the router that they would like to have a connections on particular port forwarded to them. I have no idea if the iPhone supports this protocol.

For IP lookup you may consider using DynamicDNS if the two hosts are known ahead of time OR as stated above a custom web service if they are not (i.e. friends exchanging hostnames vs. strangers discovering each other)

Chris Nava
A: 

I have a very strong feeling (but no evidence) that the iPhone address is going to be non-routable. Of course it actually depends on the operator but AT&T give out 10.x.x.x addresses.

Apple have sample (desktop) code that uses Bonjour to implement portmapping on a WiFi router that supports UnPnP.

Roger Nolan
A: 

This is an old thread, but I just came across it. You could use a STUN server (or NAT-PMP) to get the public IP address/port combo and then SMS it over to the other phone. So long as you open a listening port on the first device, then your second device can just connect directly using the publi IP address/port you sent to it in the SMS (SMS push registry can automatically wake up your app on the second device to do this)

Dan
+1  A: 

I have run into this issue with an app I was developing. I was trying to connect to a PC cell card from an iPhone. The problem is that AT&T doesn't enable mobile termination for iPhones so it is physically impossible for two phones on an AT&T network to connect to one another. This is straight from an AT&T antenna engineer.

I was able to get this to work by having the iPhone VPN into a server and then connect to the PC cell card but other than that, it wouldn't work.