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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.