Re: Cheap Ping
| From: | Jürgen Wind | Date: | Sat, 14 Apr 2007 01:33:59 +0000 |
| Subject: | Re: Cheap Ping | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-252922@lists.php.net to get a copy of this message | ||
Jim Lucas-2 wrote:
>
> Richard Lynch wrote:
>> On Fri, April 13, 2007 12:20 am, Jim Lucas wrote:
>>>> Is there some way I can set a shorter limit?
>>>
>>> http://us.php.net/manual/en/function.stream-set-timeout.php
>>
>> Bzzzt.
>>
>> That's for an already-opened stream.
>>
>> :-)
>>
> I figured that the op would figure that part out and possibly change his
> approach to use fsockopen
> instead of file_get_contents
>
> --
> Enjoy,
>
> Jim Lucas
>
> Different eyes see different things. Different hearts beat on different
> strings. But there are times
> for you and me when all such things agree.
>
> - Rush
>
>
the following from
http://de.php.net/manual/en/function.socket-create.php#60438
(slightly modified) works well on my test laptop(w2k,php5.2.1):
function ping($host) {
$package = "\x08\x00\x19\x2f\x00\x00\x00\x00\x70\x69\x6e\x67";
$sock = socket_create(AF_INET, SOCK_RAW, 1);
/* timeout 0.2 seconds */
socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 0,
'usec' => 200000));
socket_connect($sock, $host, null);
socket_send($sock, $package, strlen($package), 0);
$TS = microtime(1);
if(@socket_read($sock, 255)) {
return microtime(1) - $TS;
} else {
return -1;
}
socket_close($sock);
}
--
View this message in context: http://www.nabble.com/Cheap-Ping-tf3569802.html#a9989132
Sent from the PHP - General mailing list archive at Nabble.com.