English 中文(简体)
How can a while() loop not stall when using fsockopen() and fgets() in PHP?
原标题:

This is the basic connection code for a small PHP IRC bot. The problem is that it seems that the while() loop does not progress beyond fgets() until it receives data from the IRC server. I want the while() loop to iterate regardless if the IRC server has not yet sent data. Is this possible?

$socket = fsockopen($config[ irc_server ], $config[ port ]);
while (1) 
{
    $data = fgets($socket, 128);
    echo  [RECEIVE]   . $data;
    $recv = explode(   , $data);

    if ($recv[0] ==  PING )
    {
        send( PONG , $recv[1]);
    }
}
问题回答

Check out the socket_* functions including this one:

socket_set_nonblock

You can can also pass a non-block flag to socket_recv

Here is the obligatory quick and dirty example (no error checking)

/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = socket_connect($socket, $address, $service_port);
while (false === ($bytes = socket_recv($socket, $buf, 2048, MSG_DONTWAIT))) 
{ /* do stuff while waiting for data */ }

Take a look at ftell. Here is a related example from the php documentation:

#!/usr/bin/php4 -q
<?
#following will hang if nothing is piped:
#$sometext = fgets(STDIN, 256)

$tell = ftell(STDIN);

if (is_integer($tell)==true) 
  {echo "Something was piped: ".fread(STDIN,256)."
";}
else 
  {echo "Nothing was piped
";}

?>

stream_select() can tell if data is available for reading on the socket. But fgets() doesn t return until there s a line break or the stream ends. So you d have to use fread() instead and split the data yourself.

btw: You might also be interested in the PEAR::Net_SmartIRC package.

The problem is that it seems that the while() loop does not progress beyond fgets() until it receives data from the IRC server.

I don t see how that s a problem. What do you want that code to do if it receives nothing?

At the moment, it ll just loop around back to the fgets again, so you re just burning cycles busy-waiting for no good reason. Blocking is the correct behaviour in this instance.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签