Question about socket_read()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CMaLLeTTe
Forum Newbie
Posts: 4
Joined: Thu Oct 23, 2003 5:14 pm

Question about socket_read()

Post 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?
CMaLLeTTe
Forum Newbie
Posts: 4
Joined: Thu Oct 23, 2003 5:14 pm

Post by CMaLLeTTe »

delete this topic, i've been answered :)
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Please post here the solution. It will be useful to someone else someday.

Regards,
Scorphus.
CMaLLeTTe
Forum Newbie
Posts: 4
Joined: Thu Oct 23, 2003 5:14 pm

Post 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);	
}	
}
Post Reply