views:

37

answers:

3

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.

+1  A: 

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");
}

?

MartyIX
My code: http://pastebin.com/gk7BajM1It echoes "Valid socket resource".
Angelo Geels
irCmd does not know the $socket variable. It is the problem I think.
MartyIX
And on what line is the error?
MartyIX
Read my answer to this question, I fixed it.
Angelo Geels
I suggested it first :-) and now off I go.
MartyIX
A: 

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.

Artefacto
fwrite($socket,$cmd,strlen($cmd)); tells me I've passed an "Invalid resource".
Angelo Geels
A: 

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!

Angelo Geels
And that's why globals are BAD.
LukeN
Why don't you simply make it a parameter of irCmd? It is a solution for now but consider what happens if you want to be connected to more servers.. You'll have to rewrite it therefore it's better to write it properly in the first place.
MartyIX