note 39935 deleted from function.fsockopen by irchtml

From: Date: Tue, 17 Feb 2004 14:06:56 +0000
Subject: note 39935 deleted from function.fsockopen by irchtml
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-65279@lists.php.net to get a copy of this message
Note Submitter: Toppi@kacke.de ---- About problems how to detect the serverside timeout during read/write actions, i use the "stream_select" function instead of socket_get_status. <? class sock { var $fp; function sock(){ // } function connect($host,$port,$timeout = 5){ $this->fp=@fsockopen($host, $port, $errno, $errstr, $timeout); if (!$this->fp){ return false; } return true; } function is_Connected() { //Returns 0=no longer active / 1 = active return !feof($this->fp); } function can_read (){ $set = array($this->fp); $ret = stream_select($set, $b = NULL, $c = NULL, 0); if($ret === FALSE){ echo "Error: stream_select() failed \n"; return False; } if($ret > 0){ //Socket has something for us return True; } //Socket available but has nothing to read return NULL; } function write ($string) { if (feof($this->fp)) { //Cant write to server (ping out prolly) return false; } if ($string && !fputs($this->fp, $string . "\n")) { //Cant write to server return false; } return true; } function read ($len = 4096) { if(!$this->can_read()){ //Nothing to read from server return false; } return fgets($this->fp,$len); } } ?> <? //Example $sock = new sock(); if ($sock->connect("host","port")){ $data = ""; while(TRUE){ if(false === ($data = $sock->read()) ){ break; } echo "$data<br>"; } if(!$sock->write("Hallo")){ echo "Error write"; exit; } if(!$sock->is_Connected()){ echo "Server is outpinged"; exit; } }else{ echo "Cant connect"; } ?> It works fine for me so far. Maybe it helps you out :) Toppi

« previous php.notes (#65279) next »