Can't connect to local socket, connection refu

2019-08-17 03:10发布

问题:

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?

回答1:

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



回答2:

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.