无法连接到本地套接字,连接被拒绝
我已经在这个问题上呆了几天了,但无法解决这个问题。我也把它放到了 Ubuntu 论坛上,但没有听到任何消息。基本上,我在 /tmp/mysockets 中有一个本地套接字,我在 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_connect($sock, $sock_str) === false)
{
echo "socket_connect() on " . $sock_str . " failed: reason: " . socket_strerror(socket_last_$
socket_close($sock);
exit();
}
这给了我
警告:socket_connect():无法连接 [111]:连接被拒绝 在 /var/www/myscript.php 第 66 行 socket_connect() 上 /tmp/mysockets/sock 失败:原因:连接被拒绝
现在,我认为这可能是一个权限问题,但我已经 chmod 777'ed /tmp、mysockets 和 sock,这并不重要。可能是什么问题?
I've been at this for a few days now and just can't crack the problem. I've also put it up on the Ubuntu forum and heard nothing. Basically, I have a local socket in /tmp/mysockets which I create successfully in a php script--
if (($sock = socket_create(AF_UNIX, SOCK_STREAM,0)) === false)
{
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
exit();
}
but to which I cannot connect, via
if (socket_connect($sock, $sock_str) === false)
{
echo "socket_connect() on " . $sock_str . " failed: reason: " . socket_strerror(socket_last_$
socket_close($sock);
exit();
}
This gives me
Warning: socket_connect(): unable to connect [111]: Connection refused
in /var/www/myscript.php on line 66 socket_connect() on
/tmp/mysockets/sock failed: reason: Connection refused
Now, I thought this might be a permissions issue, but I've chmod 777'ed the /tmp, mysockets, and sock, and it doesn't matter. What could be the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在
socket_create()
之后使用socket_bind()
和socket_listen()
。You need to use
socket_bind()
andsocket_listen()
aftersocket_create()
.所以,事实证明,应该侦听此套接字的进程崩溃了——我正在远程执行此操作(而且我刚刚习惯远程编译/调试),所以我一开始没有看到这一点。抱歉各位,我错误地描述了问题。 @duskwuff你的想法是正确的。
So, it turns out that the process that was supposed to be listening for this socket crashed--I'm doing this remotely (and I'm just getting used to remote compiling/debugging) so I didn't see this at first. Sorry guys I described the problem incorrectly. @duskwuff you had the right idea.