How can I simplify addressing an iOS device using Bonjour? - iphone

I would like to make an application for iOS devices that would communicate with a special type of remote device using HTTP POST requests. The problem is that this device would actually act in the role of a client, while iOS device has to be the server accepting the requests. The next problem, host name and port has to be configured statically at the device sending the requests.
While I'm aware it's possible to give it a try using Dynamic DNS services and UPnP/IGD or NAT-PMP to overcome the problem with iOS device being behind the NAT router, I was wondering whether using Bonjour could simplify the problem in some way. I'm quite a newbie in networking so I'm looking for the direction and want to avoid research of Bonjour if it's not going to bring me anything.
Any other idea of how to solve the problem is appreciated as well.

I had a think about this. IMHO you can't guarantee you'll always be able to expose an iOS based service over a WAN (although it's possible) so this isn't practical.
The best solution I suggest is using uPNP to open the device behind a router to the outside world, then have it send an iOS push notification to the iPhone with the details (hostname, ip, port, etc).
The iPhone would then would then have to set up some kind of persistent connection with the firewalled device to receive data.

Bonjour is a good system to discover and connect to services/clients, but only on your local network. I don't think it is a good idea to try it over the internet.

Related

Connecting to peers and how to read/write to a device on another network

I think I have a fundamental misunderstanding of how networking and peer-to-peer applications work, I would be really appreciative if someone could help clear things up for me.
I am designing a small peer-to-peer file sharing application, and it seems like I can't connect to a device on another network without forwarding a port from the router to that device. This seems rather tedious; if everyone using the application has to forward a port to their device it would make the application unpractical. Is there some way that p2p apps communicate with each other directly, or are p2p applications actually just a set of server and client applications working together to form a network?
In other words, do I need to forward a port on one machine and use it as a server initially, then as the network grows larger just hope that others will do the same? Or is there something I don't know / don't understand about how p2p communication works?

App communication in nearby devices without user intervention

Is it possible to communicate two instances of an app:
In nearby devices (Android or iPhone)
Without user intervention (aside from starting the app once)
Without internet (but not network) connection?
How?
Android to Android?
iPhone to iPhone?
Android to iPhone and vice-versa?
Let us ignore the issues associated with implementing the network communication at the application level and focus on the system design problem your question raises.
You want an application on one device to connect to another associated application on another device without any user intervention/interaction beyond launching the device. In order for this to work your system is going to need a 3rd party separate from the two devices/applications that provides a lookup service.
What will need to happen is each application will need to communicate with this service, ask for the data regarding other device it should connect to, if it exists, and then attempt that connection. This is basically how a bittorrent tracker works.
Keep in mind that there's quite a bit more involved in the implementation than that simple scenario suggests. For example, the proper registering/unregistering of devices, registration timeouts, security, etc.
Unless I misunderstand your question, you should be able to just use standard sockets (TCP or UDP depending on your needs) for communication between any/all devices. For iPhone, you could probably use the NSStream classes and for Android you'd use the Socket classes.
Can the new NFC feature be an answer to this?

if I have an iphone's IP address is there anyway I can communicate with it?

are there any services running by default on an iphone that would let another ip address communicate with it if the iphone's ip is known?
Yes, technically, you can. There are a couple of issues though:
Mobile IP addresses usually change
When the phone is in sleep mode, its network interface is probably not reachable.
So I don't think you can rely on that for initiating a communication with the phone.
There aren't many options out of the box on a non jail-broken phone. However, you can write your own client application that runs on the phone. It accepts or initiates the remote connection.
And in that case, the protocol is entirely up to you.
If you jailbreak your phone, you can install the OpenSSH package and have SSH and SFTP access to your phone.
I don't know exactly what you want to do with your app, but you can consider create a thread detached from the main one (using gcd) and use it to send/receive informations from a server using JSON objects for example. Sure you can use sockets and write a client or something like that.
I suggested a new thread because you can put it running forever (on a enterprise app, I don't know if Apple will approve a thread running on an App for AppStore).
I had to develop an app with a consistent client/server relationship and it was the way I got it working.

How can I send simple data from an iPhone to another iPhone, or an Android?

I'm just getting started with mobile development, and after considerable searching I'm still confused about sending a message from my iPhone to another or an Android. For instance, in building a simple Tic-Tac-Toe game, if I want to notify the other phone of a move, what is the best way to do so? Wifi, bluetooth, 3G? And how?
Many methods seem to point to communicating through a web server, but I'd rather send them directly from phone to phone if possible. (this is for native apps of course)
If you don't want to have a server (or even if you do! It depends!) it sounds like you will want to use TCP sockets. Apple doesn't have a generic bluetooth API to use with iPhone-to-android communication and you'll have TCP access anyway with 3G. A 3G network connection is very similar to wifi and you should be able to check for the difference as needed with little change to your overall networking code. I'm not sure if bonjour is supported on android, but I bet someone has ported it for similar reasons already, and it is supported in iOS. Apple has a networking section for iOS that you may find informative.
Additionally if you want to stay within the iOS device only realm you could use GameKit to communicate via bluetooth.
Hope that helps some.
The easiest way is to create a web server with an API and the clients poll the server for new moves (or the server pushes the client an update). This is the only way you're going to be able to do it over 3G unless you can figure out the IP addresses of both phones (do phones even keep the same IP address for a long period of time?) and open up sockets between them and let one phone be the server and the other be the client (could cause potential cheating).
WiFi is not always there - by limiting your users' options to areas with access points, you'll lose sales. Not sure about Bluetooth - how prevalent is it on handsets, how much of the API is exposed. Your best bet, IMHO, is 3G. With some kind of a central presence server (Web or otherwise), with optional proxying. Read up on peer-to-peer networking.
As far as i know does the iphone only support incoming messages over push notifications.
This means, that you have to update the information, by requesting it from web services etc.
The bluetooth API of the iphone doesn't support this either.

Question about using Bonjour or other methods to secure a connection with a non computer device

So I have a project I am working on, and I would basically like to send a basic yes/no command from the iPhone to a wifi enabled device that is also connectedto the network on the same router. This other device is not a computer, so I am wondering if I can still use Bonjour? In order to use Bonjour, do both of the communicating devices need to have some sort of a script installed and running that is allowing them to communicate? What would be the best way to connect to this device that is connected to the router, and then to send information/commands to it? I could really use some help finding a direction, because once I identify which tools I need to use, I can research them out and get it done (maybe). If anyone could offer any insight/suggestions whatsoever, it would be greatly appreciated!! Thanks in advance
Bonjour is a local service discovery protocol based on multicast DNS. Even if your device supports it you still need some piece of software on the device to listen to your commands.