Linux 无法连接到本地套接字,连接被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7211827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:54:30  来源:igfitidea点击:

Can't connect to local socket, connection refused

phplinuxsocketsconnection

提问by Matt Phillips

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--

我已经在这几天了,只是无法解决这个问题。我也把它放在 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();
}

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

警告:socket_connect():无法连接 [111]:连接被拒绝在 /var/www/myscript.php 上的第 66 行 socket_connect() on /tmp/mysockets/sock 失败:原因:连接被拒绝

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?

现在,我认为这可能是权限问题,但我已经 chmod 777 修改了 /tmp、mysockets 和 sock,这并不重要。可能是什么问题呢?

采纳答案by Matt Phillips

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.

所以,原来应该监听这个套接字的进程崩溃了——我正在远程执行此操作(我只是习惯了远程编译/调试),所以我一开始没有看到这一点。对不起,我错误地描述了问题。@duskwuff 你的想法是对的。

回答by Narf

You need to use socket_bind()and socket_listen()after socket_create().

您需要使用socket_bind()socket_listen()之后socket_create()