Re: NSURLSession + Bonjour to do peer-to-peer file transfers?
Re: NSURLSession + Bonjour to do peer-to-peer file transfers?
- Subject: Re: NSURLSession + Bonjour to do peer-to-peer file transfers?
- From: Jens Alfke <email@hidden>
- Date: Thu, 26 Dec 2013 18:04:55 -0800
On Dec 25, 2013, at 7:26 AM, Eric Wing <email@hidden> wrote:
> I can publish my iPhone via Bonjour and make my Mac discover and
> resolve the address. I try to create a NSURLSessionDownloadTask on my
> Mac and a NSURLSessionUploadTask on my iPhone.
That won’t work, because both of those are acting as HTTP clients (one sending a GET, the other probably a PUT.) Unless one of them acts as a server instead, they can’t talk to each other.
All Bonjour does is advertise the availability of a listener/server/service on a port. But it doesn’t actually _create_ that listener — that’s your responsibility. You have to run some kind of server code that will bind to that port, accept connections, and speak the desired protocol.
The problem is, Apple doesn’t include any high-level APIs for running such a server. Most commonly people want to serve HTTP. You can cobble together a crude HTTP server using CFStream and CFHTTPMessage, but there are a lot of details involved in making one that will work for real (i.e. well enough to talk to a browser.) Or you could build a very simple custom protocol if all you need to do is transfer a blob of data.
Take a look at Apple’s PictureSharing sample code, which shows how to set up a basic server and client to send data.
If you want to run a real HTTP server, the best package I know of is CocoaHTTPServer (find it on Github.)
—Jens
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden