从 PHP 脚本绑定到套接字时出现问题
我对 PHP 很陌生,所以这可能很简单。无论如何,我正在尝试在 PHP 脚本(由 Web 客户端激活)和本地进程(用 C++ 编写)之间创建双向通信。 PHP 脚本应该向 C++ 进程发送一些信息,然后等待响应。我的问题是,建立这种通信的唯一方法似乎是使用 socket_bind,但是当我这样做时,它会失败并出现“地址已在使用中”错误。有问题的套接字文件 /tmp/sock 已由连续运行的 C++ 进程创建(无法由 PHP 脚本启动)。如果我使用 socket_connect 并向 C++ 进程写入一些内容,那就可以了;但我需要先绑定,然后才能从 PHP 脚本监听该套接字。这是我的代码:
<?php
error_reporting(E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();
//Adapted from http://www.php.net/manual/en/sockets.examples.php
if (($sock = socket_create(AF_UNIX, SOCK_STREAM,0)) === false)
{
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
exit();
}
if (socket_bind($sock, '/tmp/sock') === false) //Fails here
{
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
....
谢谢--
I'm very new to PHP so this might be something simple. Anyway, I'm trying to create 2-way communication between a PHP script (activated by a web client) and a local process (written in C++). The PHP script should send some information to the C++ process and then wait for a response. My problem is that the only way to set up this kind of communication seems to be to use socket_bind, but when I do, it fails with the 'address already in use' error. The socket file in question, /tmp/sock, has already been created by the C++ process, which is running continuously (it can not be launched by the PHP script). If I use socket_connect and just write something to the C++ process, that works just fine; but I need to bind before I can listen to that socket from the PHP script. Here's my code:
<?php
error_reporting(E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();
//Adapted from http://www.php.net/manual/en/sockets.examples.php
if (($sock = socket_create(AF_UNIX, SOCK_STREAM,0)) === false)
{
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
exit();
}
if (socket_bind($sock, '/tmp/sock') === false) //Fails here
{
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}
....
Thanks--
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 C++ 和 PHP 代码必须将套接字标记为共享 (SO_REUSE_ADDR)。
Your C++ and PHP codes have to mark the socket as shared (SO_REUSE_ADDR).
事实证明,所需要的只是socket_read()。我没有意识到您可以在同一个套接字上执行这两个操作(当它仅连接且未绑定时)。 PHP socket_read 的用户 hamishcool3 有一个很棒的实用函数,可以从套接字直到到达特定字符(尽管它可能比标准字节限制读取慢)。
It turns out that what's needed is simply socket_read(). I didn't realize you could do both operations on the same socket (when it was merely connected and not bound). User hamishcool3 at PHP socket_read has a great utility function for reading from a socket until a particular character is reached (though it is probably slower than a standard byte-limited read).