Jump to content

socket_read timeout??


php_guy

Recommended Posts

Is it possible to implement a "timeout" in the socket_read() function?

 

I'm using the following code (from PlanetSourceCode.com)

 

 

    $timeout = 10;
    
    // And off to the sockets
    $socket = socket_create(AF_INET, SOCK_RAW, 1);
      
    $time = time();
    
    while( !@socket_connect($socket, "machine888", null))
    {
	$err = socket_last_error($socket);
	if ($err == 115 || $err == 114)
	{
		if ((time() - $time) >= $timeout)
		{
			socket_close($socket);
			die("Connection timed out.\n");
		}
		sleep(1);
		continue;
	}
	die(socket_strerror($err) . "\n");
    }

    $startTime = microtime(true);
    socket_send($socket, $package, strLen($package), 0);
    if (socket_read($socket, 255)) {
    echo round(microtime(true) - $startTime, 4) .' seconds';
    }
    socket_close($socket);
    
    echo('all done');

 

 

So, my script freezes at socket_read (this is verified by incrementally removing/adding lines of code, etc.). Is there a way I can make it time out?

Link to comment
https://forums.phpfreaks.com/topic/59107-socket_read-timeout/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.