Tutorial :Web server on port 80 on iPhone



Question:

I've been working on a project for awhile and it's got a built-in HTTP server which runs on port 8080. The users are told to access the device via e.g. http://192.168.1.4:8080/ -- works great. Recently I realized that applications CAN use port 80 to remove the need for ":8080", though if I try to set the port to 80 I get a crash with "General CFSocket error".

Any ideas how to enable port 80 for a web server on an app?

A few screenshots where this is happening:

First -- on the iPad, the app is showing the URLs where you can access it.

iPad http://enrogue.com/port80/ipad.jpg

Second -- Firefox, by IP:

FF by IP http://enrogue.com/port80/firefox_byip.png

The above are from a real app on the store, it's not jailbroken magic or anything. I know that ports < 1024 are reserved for the admin on UNIX systems, so the above app is obviously doing something specific to get access to the port.


Solution:1

You can bind to port 80 on the device's IPv4 interface but not the IPv6 interface and not in the simulator. You'll need to modify your socket code to only listen on the IPv4 interface, for the simulator you can conditionally use a different port:

#if TARGET_IPHONE_SIMULATOR      [httpServer setPort:8080];  #else      [httpServer setPort:80];  





        
Previous
Next Post »