如何建立从 iPhone 到 Apache 服务器的套接字连接并通过 PHP 进行通信?
我正在开发一款依赖于 LAMP 服务器的 iPhone 游戏。我想创建一个基于“事件”的系统,其中 apache 服务器将事件发送到 iphone。
为此,我正在考虑使用“CFStreamCreatePairWithSocketToHost”连接到apache服务器的端口80。
我能够成功连接到服务器并通过 iPhone 打开读写流,但我不确定如何使用从 LAMP 服务器运行的 PHP 将数据发送到 iPhone。
我想我可以在 php 中使用 fsockopen 打开套接字连接并将数据写入该套接字。我厌倦了运行这段代码
$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";
}
但是,我没有看到 iPhone 上有任何内容被读取。知道发生了什么,或者如何实现这一点?
谢谢!
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. 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,使用 PHP 来侦听传入连接并不是最佳选择,这并不是它的构建目的。是否没有办法使用基于“正常”HTTP 的通信通道,向 URL 发出简单请求并以您喜欢的任何格式解析返回的数据?
无论如何,请查看
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
HTTP 并不是真正的流协议,您可以发送请求并从中获取响应。您可能想要查看 apache/php 以外的其他东西来满足此要求
...或者听取 Pekka 的建议并将您的要求分解为简单的请求响应。即定期轮询您的服务器,看看是否有任何数据等待 iphone 处理...注意,我并不提倡这样做,因为它对网络或程序员来说不太令人愉快,但它是可能的。
另一种方法是构建您自己的服务(不要使用 apache),该服务将接受您的 iphone 可以连接到的特定端口上的连接,并以这种方式进行通信 - 如果您愿意,您甚至可以通过这种方式运行 PHP 脚本(但是请阅读Pekka 提供的链接)。
另一种方法是将服务器构建到您的 iphone 应用程序中,您的网络服务器可以连接到该服务器 - 然而这有点愚蠢,因为您的服务器可能比您的 iphone 在线时间更多(因为 iPhone 的应用程序不会在后台运行,如果它是未越狱,并且 iphone 4.0 尚未向公众发布)。
尽管如此,还是有选择:)
马特
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
使用“拉”模型而不是“推”模型...
浏览器通常被设计为无状态的。有多种方法可以解决该限制,但不要执行您所描述的操作。
因为,服务器永远不会将数据“推送”到客户端。这意味着服务器必须了解数据的存在、位置和介质才能将数据传输到客户端,而它们通常不会这样做。
而不是尝试让服务器将事件推送到 iPhone 上的客户端。让服务器存储状态并让 iPhone 使用 AJAX 频繁轮询应用程序状态。
如果您想要一个在服务器和客户端之间具有直接 1-1 连接的应用程序,那么 LAMP/PHP 是错误的方法,因为您需要在服务器和 iPhone 上运行 LAMP 才能完成您所描述的任务。 IE 中,您无法将数据从 HTTP 服务器“推送”到 HTTP 客户端,您只能将服务器请求的数据返回到客户端。
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.