What classes do I use to make an iPhone act as a server?
Asked Answered
D

3

6

I'm looking for an easy way for users to download content from an iPhone to their computer. I've seen other apps that actually turn the iPhone into a server and give the user an IP address to navigate to on their computer. I've glanced at some Apple samples, but nothing looked too much like what I was going for.

So what's the easiest way to make a server that listens on TCP port 80 (even better, an HTTP server) and sends responses? Hopefully using Objective C classes, but I can make a wrapper if there isn't anything available.

Dustydusza answered 26/1, 2009 at 2:47 Comment(1)
This is an excellent question. I look forward to hearing an answer.Ozoniferous
V
4

Google Toolbox for Mac has a class called GTMHTTPServer.

Deusty Designs has a project called CocoaHTTPServer.

You can't use port 80 because it requires root access.

Verbenia answered 26/1, 2009 at 6:8 Comment(2)
Deusty Design's project: Incredibly helpful, and simple to use!Dustydusza
By the way, IPv6 is not available on iPhone OS. But you may find that it works in the Simulator. So, beware.Verbenia
D
2

Cocoa provides lots of support for client-side networking but not a lot for server-side.

At the lowest level, you can use normal BSD sockets.

The next level up is CoreFoundation (plain C but using Cocoa-like types). Relevant CoreFoundation APIs are CFNetwork, CFSocket and CFStream (CFStream is a file-stream that can have its source through a network -- it is not a System V style network Stream).

In Objective-C, you can look at NSStream which is the Objective-C equivalent of CFStream.

Dote answered 26/1, 2009 at 8:9 Comment(0)
B
0

There is a good O'Reilly article on setting up a simple server.

How to Write a Cocoa Web Server

He uses NSFileHandle and NSSocketPort to setup a listener and handle requests. He also gets into some of the detail work of using the BSD sockets directly. I have had luck with this approach in the past.

This was written back in 2006, and I haven't used this on an iPhone project yet, but the classes he uses are pretty common. I would give it a fighting chance of working for an iPhone project.

Bequeath answered 26/1, 2009 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.