Yes, peek() and pending() are intended as non-blocking calls that you can use to get an idea of what will happen if you issue a read().
If I understand your question correctly, you desire a more efficient way to wait until the response from the server is available. This is typically done by:
while($data = $sock->peek()){
if($data eq $what_you_want){
&DO_YOUR_STUFF;
last;
}else{
sleep 1;
}
}
It's not totally necessary to have that short sleep, but typically you want some type of pause so that your script doesn't get into a tight loop and hog the cpu.
If you don't know what data you expect back, you can use pending() instead of peek() to see the amount of data waiting to be read(). | [reply] [d/l] |
Hrm. For some reason Peek keeps giving me "0" and it falls out if the while loop right away:
while($response = $sock->peek())
{
if ($response ne '')
{
#verify the data
print "Server Response: $response\n";
$response = (string2hex($response));
print "Server Response: $response\n";
last;
}
else
{
sleep 1;
print "Waiting for a response!\n";
}
}
| [reply] [d/l] |
You're using a string operator on a number in your if statement. Try changing
if($response ne ''){
...
}else{
...
}
to
if($response > 0){
...
}else{
...
}
| [reply] [d/l] [select] |