Filed under: Cool tools, Hacks, iPhone
MDNS and caffeine: How I got Bonjour running on my iPhone
See this picture? It's a picture of my iPod touch. But here is the interesting thing. The picture you see is actually a screen shot from my iPod touch. As you can see from the iPod logo, it has no on-board camera. Instead, it's subscribed to a Bonjour service on my iPhone and using its camera instead to take a picture of itself.
Say what?
Holy. Cow. Do I ever need some rest. I just spent most of last night figuring out how to get Bonjour (aka mDNS) running on my iPhone. Bonjour offers configuration-free networking technology. Computers (and iPhones) automagically broadcast their services, which you can then listen for and take advantage of. This has been a long-term project that I've been plugging away at for months.
So what makes this exciting? With Bonjour, your iPhone and touch can talk to your Mac. They can talk to printers or to each other. They can browse for available services or provide their own as a server. What I put together was a pair of applications: PicSend and PicListen. These apps allow you to subscribe to a camera on another person's iPhone. Just tell that iPhone to snap a picture and within seconds that picture appears on your own screen.
PicSend and PicListen
You can download a copy of PicSend and PicListen from my FTP site. They work like this:
Install PicSend on any iPhone. This is the server app and needs to run on a unit with a working camera.
Install PicListen on either a touch or an iPhone. It subscribes to the PicSend service and tells the iPhone to snap pictures and send them on over.
PicSend has one button that freezes and unfreezes the camera display. When stopped, the live preview ends and a single image displays until you resume.
PicListen has one button too. It's labeled "snap". Tap it to tell the server to take a picture. If you're holding the server you know when someone snapped a picture--you'll hear the camera shutter "click".
PictureSharing and PictureSharingBrowser
Don't have two iPhones on-hand? Apple provids two sample code programs that tie right into these utilities. PictureSharingBrowser and PictureSharing both run on the Mac. Browser is the client, PictureSharing is the server. They're using the same Bonjour protocol so if you set your service to PicBrowser, you'll be able to send pictures to your iPhone or see pictures from the iPhone on your Mac.
To send a picture to, say, your iPod touch, just drop it into PictureSharing and press Start. PicListen will scan for Bonjour services, find the picture, and display it.
Similarly, PictureSharingBrowser allows you to see the pictures you snap on your iPhone.
Programming
Normally programming in Bonjour is really easy. That's because Apple offers some fabulous Objective-C classes (like NSNetServices) that take care of most of the messy bits. In the world of Apple, everything that is Cocoa and Objective-C is good and lovely and yummy. Unfortunately, although the Bonjour daemon comes pre-installed on iPhone, the supporting classes did not.
That leaves us in the world of Core Foundation. Everything that is CF is bad, and evil, and C++-like, and just...messy. Everything you can do in heavenly Objective-C, you can do in evil Core Foundation. I spent hours laboriously porting Apple's CFNetServices samples to iPhone.
So what's the big deal?
Sending pictures back and forth between iPhones isn't a huge deal until you realize that you're not limited to pictures. With Bonjour, you can send files, data, sounds, videos, anything that transfers from one computer to another. I'm hoping to adapt the code to work next with sound. Sure, you can do a lot better at Target in terms of buying sub-$400 walkie-talkies, but again we're dealing with baby steps here. I'm hoping that the iPhone will soon be able to "discover" printers in its area and offer to print out pictures or webpages or people can use Bonjour to play interactive games. The possibilities of social computing with iPhones, iPod touches and Bonjour are practically limitless.
Got some ideas, suggestions or requests? Let me know in the comments.
Thanks to GeeKdLL and Drudge for testing.


![TUAW [Cafepress]](http://www.blogsmithmedia.com/www.tuaw.com/media/tuaw-cafepress-promo.png)


Reader Comments (Page 1 of 4)
Jeedee said 6:44PM on 10-22-2007
Awesome! Keep up the good work!
Reply
Lcs said 6:46PM on 10-22-2007
Airtunes?
Reply
Ben said 6:48PM on 10-22-2007
In my testing, both with mDNSResponder and avahi, the network stack refused to deliver multicast packets to the application layer. How did you work around this?
Reply
PK said 6:59PM on 10-22-2007
Wow, I'm very impressed, Erica. I'll most likely never use this on my virgin iPhone, but I'm in awe of your programming skills and passion for coming up with novel things to do on this platform. It's folks like you that make my Ubuntu install and other open source software I have work as well as it does - best of luck as you tackle more cool things for the iPhone/iPod touch platform.
Reply
Micah Neumark said 7:04PM on 10-22-2007
Question, does the Airport Express use mDNS? Cause if it does, is it possible to use your work to hijack iPhone/iPod Touch sound and transmit it to the Airport Express? I have been waiting to be able to do this for a while. It would be a killer feature!
Reply
matt said 7:10PM on 10-22-2007
i see that the iPod touch is connected to your computer . . . have you thought of configuring it to run Bonjour wirelessly? then you could access files and share stuff throughout your house, without the need to go to the computer(okay, maybe this is just me being lazy, but i can see a business application here), so that you have instant access to stuff. business wise, instead of having to get laptops for all the employees, just get iPods or iPhones that can access company information over the network. save money and time. I think that it may be a viable idea, it would take A LOT of programming and time to get it to run smoothly, but i can see it on large campuses and whatnot!
Reply
Jacob said 7:11PM on 10-22-2007
Can PicSend be installed on a regular mac? or does it have to be an iPhone?
Reply
Erica Sadun said 7:10PM on 10-22-2007
Matt: Bonjour is wireless. I was just tethered for battery power.
Reply
Erica Sadun said 7:12PM on 10-22-2007
Jacob: PicSend is only for iPhone. The Mac equivalent is PictureSharing. See the link in the article to download a copy.
Reply
JMG said 7:16PM on 10-22-2007
You have so much white plastic stuff on your desk, I just don't know if I can take it.
Reply
ecume said 7:20PM on 10-22-2007
wow - erika. I was running into the same issues as Ben (above). Would you post your code for the browser? It would save some of us a LOT of headache. and congrats!
Eric
Reply
matt said 7:22PM on 10-22-2007
@Erika: ahhh okay. wasn't sure. thanks for clarifying!
Reply
Simon Andrews said 7:24PM on 10-22-2007
I think what should be in the plan is to have it wirelessly sync with a computer, so you don't need to plug the iPhone or iPod touch in to sync it. I'm sure it's possible ;)
Reply
ball said 8:01PM on 10-22-2007
wow. well done! this is quite something.
Reply
me said 7:41PM on 10-22-2007
So will "Bonjour" show up in Installer.app soon? like with a GUI to let you set your name?
will this mean i can type:
ssh root@myphone.local ??
rather than typing some address??
And can i type:
http://mycomputer.local
into MobileSafari, to get to my locally hosted web site from my iphone?
pleeeeeeeez?
Reply
Furies said 7:41PM on 10-22-2007
Wow! can I get a w00t w00t for Erica here? CF is a very messy framework, and looks and acts more like hardcore C. I'm curious though, what is needed to port it to the iPhone? I thought it was running an OS X just like my iBook now...
Reply
A. Jay said 8:20PM on 10-22-2007
This opens up great potential. First, props on the first step you have achieved so far. I was surprised not to see any iPhone additions accompanying the Leopard release, for example Screen Sharing. My assumptions are Apple™ will trickle out there wonderful implementations to 'WOW' their current and future customers. To get to the point, Bonjour Screen Sharing?? To clarify I'm looking to control my home computer anywhere with my iPhone through VNC, although ill start inside the network. Also, this picture send/recieve you have already created seems to be a bare-bones video chat app, in that respect I simply mean if this were to be installed on 2 iPhones, the server turned on both and the reciever showing both images? Hope to see the future evolution of great things like Bonjour and apps when they are allowed. Best of luck.
Share and Enjoy
Reply
Greg Hartstein said 8:23PM on 10-22-2007
Great work!
I can get this to work with my Mac using picSend and PictureSharing, but am unable to do the reverse and send pics from my mac to the phone. PictureSharing fires up on the mac but the phone doesn't seem to see it.
Any ideas?
Thanks for the continued good work
Reply
Erica Sadun said 8:09PM on 10-22-2007
Greg: Make sure you've set the service name to "PicBrowser".
Reply
Carl Caum said 8:11PM on 10-22-2007
So if Apple TV has all this wireless syncing capability over WiFi, can you copy the libraries that provide that service to the iphone and get it to broadcast itself as an Apple TV to iTunes on the mac? That would be sweet.
Reply