Codecat
Codecat

Reputation: 2241

fsockopen not working?

I am trying to connect to an IRC server via PHP on a command line using Windows 7.

Everytime when running this:

$socket = fsockopen($irc_server, 6667, $errno, $errstr, 5);

$errno = 0, $errstr = "" and $socket = 'Resource id #4' (using die($socket);)

What is the cause of this, and how can I debug more into this.

The following code:

$s = fsockopen("google.com", 80, $errno, $errstr, 5);
die($errno.", ".$errstr.", ".$s);

...returns the following:

0, , Resource id #4

I can't use $socket. It says "Invalid resource" when I try to use it. Also, the PHP documentation notes that errno 0 indicates a wrongly opened socket.

Help is appreciated.

Upvotes: 0

Views: 8178

Answers (3)

Codecat
Codecat

Reputation: 2241

I fixed it.

function irCmd didn't know $socket, so I put this in front of it:

global $socket;

And it worked. Thanks a bunch!

Upvotes: 1

Artefacto
Artefacto

Reputation: 97845

The documentation says (emphasis mine):

If the value returned in errno is 0 and the function returned FALSE, it is an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket.

Since the function did not return false, the socket is valid. If you are having further problems, tells us what they are; fsockopen has returned normally here.

Upvotes: 0

MartyIX
MartyIX

Reputation: 28676

Could you show us a little more of your code?

What happens with this code:

$s = fsockopen($irc_server, 6667, $errno, $errstr, 5);
if ($s === false) {
  die($errno.", ".$errstr.", ".$s);
} else {
  // your code with socket
  die("Valid socket resource");
}

?

Upvotes: 2

Related Questions