Search code examples
phpfsockopen

PHP fsockopen doesnt return anything


I am modifying a PHP db wrapper for the redis database.

Here's how my function looks:

public function connect() {

    $sock = @fsockopen('localhost', '6379',  $errno, $errstr, 2);

    if ($sock === FALSE) {
        return FALSE;
    }
    else {
        stream_set_timeout($sock, 2); 
        return $sock;
    }
}

What I want to do is to call this function from another part in my wrapper:

 if ($this->connect() !== FALSE) {
      // Do stuff
 }

How can I get my connect function to send a FALSE when the fsockopen isn't working?

Thanks!


Solution

  • From a little ways down the fsockopen() page (have to scroll almost to the bottom):

    UDP sockets will sometimes appear to have opened without an error, even if the remote host is unreachable. The error will only become apparent when you read or write data to/from the socket. The reason for this is because UDP is a "connectionless" protocol, which means that the operating system does not try to establish a link for the socket until it actually needs to send or receive data.

    I'm going to guess that's your problem, I guess you have to do a test read/write to see if it was really successful or not.