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: Eric Wing <email@hidden>
- Date: Sat, 28 Dec 2013 14:33:01 -0800
On 12/26/13, Jens Alfke <email@hidden> wrote:
>
> 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
Thanks for the explanation Jens. I definitely misunderstood the notion
of "server" with http uploading (I thought NSURLSessionUploadTask
would be the server). This is partly why I couldn't seem to feed the
NSNetService addresses/ports into something useful for me.
Based on Stephen's reply, I had already found CocoaHTTPServer. After
several (very long) days, I have a prototype that works for me with
both NSURLSession clients and also Android/Java HttpURLConnection
based clients, all discoverable through Zeroconf.
This was a lot harder than I thought it would be though. I struggled
with lots of separate details, such as changing the CocoaHTTPServer's
file example which was a Content-Type "multipart" example into
something that uses "application/octet-stream" (which uploading a file
with NSURLSession seems to set), and then figuring out how to send the
same with HttpURLConnection and encountering things like fixed-length
vs chunked streaming mode. (I never did figure out how to send an EOF
when in chunked mode.) I was hoping both the Cocoa and Java frameworks
had advanced enough to the point where things like file serving was
trivial for a network dummy like myself, but I don't think they are
there yet. To their credit, I still managed to get something that
eventually worked, albeit with a lot more effort and ugly.
Thanks,
Eric
--
Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/
_______________________________________________
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