note 39975 added to function.fsockopen

From: Date: Tue, 17 Feb 2004 12:41:39 +0000
Subject: note 39975 added to function.fsockopen
Groups: php.notes 
Request: Send a blank email to php-notes+get-65273@lists.php.net to get a copy of this message
AWwww god....im so sorry ! Didint remove my own ip:port in the script :~( Here it comes my "final" again for the public ( if it becomes puplic at php.net) Content: fsockopen() stream_select() instead of socket_get_status() detecting tiemout. generell example of useing sockets fgets/fputs <? 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,500); 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){ echo "Read<br>"; 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"; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.fsockopen.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+39975 Delete -- http://master.php.net/manage/user-notes.php?action=delete+39975&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+39975&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#65273) next »