Page 1 of 1

Question about socket_read()

Posted: Thu Oct 23, 2003 5:14 pm
by CMaLLeTTe
Hello,
I am programming a script that uses a client socket to connect to a server. The only problem with the script for now is the socket_read().

When you do for instance:

Code: Select all

$buffer = "";
while(true){
$buffer .= socket_read($socket,255);
if ($buffer !== false) { parse_buffer($buffer) };
}
PHP will pause the script until something is received when you do socket_read(); I want to be able to do something while the socket is not receiving anything and have it check to see if it is receiving something at the same time.

Is there any way I can do something while socket_read() is waiting for data to be received? Or is there any way of knowing when I have data waiting to be read?

Posted: Thu Oct 23, 2003 6:43 pm
by CMaLLeTTe
delete this topic, i've been answered :)

Posted: Thu Oct 23, 2003 9:21 pm
by scorphus
Please post here the solution. It will be useful to someone else someday.

Regards,
Scorphus.

Posted: Sat Oct 25, 2003 4:01 pm
by CMaLLeTTe
To avoid using socket_read() when there is no data, you can use socket_select() to check if data is pending to be read.

Here's a simple code I found on php.net:

Code: Select all

function pending_data($socket){
	
$read = array($socket);
		
if (socket_select($read,$write=NULL,$except=NULL,0) == 1) {
return(true);
} else {
return(false);	
}	
}