How to establish a socket connection from iPhone to a Apache server and communicate via PHP?
I am working on an iPhone game which is depended on a LAMP server. I want to create a "event" based system where the apache server sends an event to the iphone.
For this, I am thinking of using "CFStreamCreatePairWithSocketToHost" to connect to port 80 of the apache server.
I am able to successfully connect to the server and open a read and write stream via the iPhone, but I am not sure how to send data to the iphone using PHP running from the LAMP server to the iPhone.
I think I can use fsockopen in php to open a socket connection and write data to that socket开发者_JAVA技巧. I tired running this code
$fp = fsockopen("tcp://localhost", 80, $errno, $errstr);
if (!$fp) {
echo "ERROR: $errno - $errstr<br />\n";
} else {
echo"writing to socket
"; fwrite($fp, "wwqeqweqw eqwe qwe \n");//echo fread($fp, 26);
fclose($fp);
echo "done";
}
But, I dont see anything being read on the iphone.. Any idea what's going on, or how to accomplish this?
Thanks!
Mmm, using PHP to listen for incoming connections is sub-optimal, it's not really what it was built for. Is there no way of using the "normal" HTTP based communication channels, making a plain request to a URL and parsing the returned data in whatever format you prefer?
Anyway, check out
- Run PHP script as daemon process
- Is it wise to use PHP for a daemon?
- and more: Search
HTTP isn't really a streaming protocol, you send requests and get responses from it. You may want to look at something other than apache/php to fulfill this requirement
... or heed Pekka's advise and break your requirements down to simple request responses. i.e. poll your server at regular intervals to see if there is any data waiting for the iphone to process... note I'm not advocating this as its not very pleasant on the network or the programmer, but its a possability.
Another way is to build your own service (do not use apache) that will accept connections on a particular port which your iphone can connect to, and communicate that way - you can even run a PHP script this way if you so desire (however read the links that Pekka provided).
Yet another way is to build the server into your iphone application which your webserver can connect to - however this is a bit silly as your server is probably online more than your iphone is (as the iPhone's apps do not run in the background if it is unjailbroken, and iphone 4.0 is not released to the general public yet).
Options none-the-less :)
Matt
Use a 'pull' instead of 'push' model...
Browsers are generally designed to be stateless. There are ways around that limitation but not to do what you're describing.
Because, servers are never expected to 'push' data to a client. That would mean that the server would have to be conscious of the presence, location, and medium to transfer data to a client, which they generally aren't.
Instead of trying to get the server to push the event to the client on the iPhone. Have the server store the state and have the iPhone frequently poll for the application state using AJAX.
If you want an app with a direct 1-1 connection between server and client LAMP/PHP is the wrong way to go because you'd need LAMP to be running on both the server and the iPhone to accomplish what you're describing. IE, you can't 'push' data from a HTTP server to a HTTP client you can only return data that's requested from the server to the client.
精彩评论