8

I have a small application I created to analyze the network connection. It runs from a browser and connects to a local PHP/Apache server. It then asks PHP to send a ping packet through a raw socket. THe problem is that if the host I am trying to ping isn't alive or won't answer to pings, we never get an answer from the server.

I beleave the socket request lives until apache is restarted. I have been getting mixed results from my application lately and I am blaming apache using too many sockets. Currently I have set the AJAX call's timeout and I was happy with it. But I really need to make PHP do the timeouting so that I won't have 500,000 sockets open to an unreachable host.

Some sample code:

$sockconn = @socket_connect($socket, $target, null);
if(!$sockconn)
{
    $raw['error'] = socket_strerror(socket_last_error());
    $raw['status'] = false;
    return $raw;
}

This is the function that won't timeout. I need to get it to timeout. Also PHP script execution time DOES NOT affect sockets.

I am clueless.

9
  • 1
    Seriously, use fsockopen() instead. It makes a lot of things a lot easier, it is more readily available (it is a core function that must be explicitly disabled, whereas the sockets extension must be explicitly enabled) and the 5th argument lets you define the connect timeout on a per-call basis. Commented Aug 9, 2012 at 10:52
  • 1
    In theory yes (although with the more advanced stream_socket_* functions rather than fsockopen()). In practice it would be a massive PITA. I sort of assumed you would be dealing with TCP, most of the time that's what people are doing. If what you want is a PHP-driven ICMP echo implementation, though, I've already done that... take a look at this: download.networkm.net/code/php/class.ping.1.0.tar.gz (note that it was written for PHP4, and when my OO skills left... something to be desired, but it does work I have used it a couple of times) Commented Aug 9, 2012 at 11:04
  • 1
    Oh plus there's a whole bunch of random debugging crap left in there. Just remove lines 291-296 Commented Aug 9, 2012 at 11:09
  • 1
    New! Improved! Generally easier to use and not quite as crap! download.networkm.net/code/php/class.ping.2.0.tar.gz Commented Aug 9, 2012 at 14:52
  • 1
    Licensed under LGPL, and to sum up that basically means "Yes, you can use it for whatever you like" :-) Commented Aug 9, 2012 at 19:20

2 Answers 2

15

You can set timeouts for reading and sending using the following options:

socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 1, 'usec' => 0));
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 1, 'usec' => 0));

Alternatively, you can use non-blocking sockets and periodically poll the socket to see if the remote host responded.

Sign up to request clarification or add additional context in comments.

Comments

0

Try setting default_socket_timeout.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.