note 26074 added to function.fsockopen

From: Date: Thu, 17 Oct 2002 00:28:44 +0000
Subject: note 26074 added to function.fsockopen
Groups: php.notes 
Request: Send a blank email to php-notes+get-38190@lists.php.net to get a copy of this message
I was tearing my hair out for a week trying to figure out how to do this. If you use fsockopen with a service that doesn't have an EOF, or you try to read beyond EOF or line break, PHP can hang completely. In my case, I was trying to write a class that talks to Kali servers (www.kali.net) to get a list of people on the chat server. To keep PHP from hanging due to the above, I discovered this: class kali_utils { function games_list($kali_server_ip, $kali_server_port) { $result = array(); $fp = fsockopen($kali_server_ip, $kali_server_port, $errno, $error, 30); if (!$fp) { $result["errno"] = $errno; $result["error"] = $error; } else { fputs($fp, "KALIQ"); $header = fread($fp, 5); $bytes_left = socket_get_status($fp); if ($bytes_left > 0) { $result["results"] = fread($fp, $bytes_left["unread_bytes"]); } else { $result["results"] = ""; } fclose($fp); } return $result; } } When I send the request packet, I get a response packet of length 5. Then I call socket_get_status() and use the unread_bytes key from it to know how far to fread from the socket. Works very good. I've only used this on PHP 4.2.1 so far. -- http://www.php.net/manual/en/function.fsockopen.php http://master.php.net/manage/user-notes.php?action=edit+26074 http://master.php.net/manage/user-notes.php?action=delete+26074 http://master.php.net/manage/user-notes.php?action=reject+26074

« previous php.notes (#38190) next »