381 | | switch(socket_select($r, $w, $f, $to_sec, $to_usec)) { |
382 | | case 2: |
383 | | /* connection refused */ |
384 | | $error = "refused"; |
385 | | break; |
386 | | case 1: |
387 | | /* get the end time */ |
388 | | $this->time = $this->get_time($this->precision); |
389 | | |
390 | | /* get packet response */ |
391 | | $code = @socket_recv($this->socket, $this->reply, 256, 0); |
392 | | |
393 | | /* get the error, if applicable */ |
394 | | $err = socket_last_error($this->socket); |
395 | | |
396 | | /* set the return message */ |
397 | | $this->ping_status = $this->time * 1000; |
398 | | $this->ping_response = "UDP Ping Success (" . $this->time*1000 . " ms)"; |
399 | | |
400 | | $this->close_socket(); |
401 | | return true; |
402 | | case 0: |
403 | | /* timeout */ |
404 | | $error = "timeout"; |
405 | | break; |
| 377 | $num_changed_sockets = socket_select($r, $w, $f, $to_sec, $to_usec); |
| 378 | if ($num_changed_sockets === false) { |
| 379 | $error = "socket_select() failed, reason: " . socket_strerror(socket_last_error()); |
| 380 | } else { |
| 381 | switch($num_changed_sockets) { |
| 382 | case 2: /* response received, so host is available */ |
| 383 | case 1: |
| 384 | /* get the end time */ |
| 385 | $this->time = $this->get_time($this->precision); |
| 386 | |
| 387 | /* get packet response */ |
| 388 | $code = @socket_recv($this->socket, $this->reply, 256, 0); |
| 389 | |
| 390 | /* get the error, if applicable */ |
| 391 | $err = socket_last_error($this->socket); |
| 392 | |
| 393 | /* set the return message */ |
| 394 | $this->ping_status = $this->time * 1000; |
| 395 | $this->ping_response = "UDP Ping Success (" . $this->time*1000 . " ms)"; |
| 396 | |
| 397 | $this->close_socket(); |
| 398 | return true; |
| 399 | case 0: |
| 400 | /* timeout */ |
| 401 | $error = "timeout"; |
| 402 | break; |
| 403 | } |
470 | | switch(socket_select($r = array($this->socket), $w = array($this->socket), $f = array($this->socket), $to_sec, $to_usec)){ |
471 | | case 2: |
472 | | /* connection refused */ |
473 | | $this->time = $this->get_time($this->precision); |
474 | | |
475 | | if (($this->time*1000) <= $this->timeout) { |
476 | | $this->ping_response = "TCP Ping connection refused (" . $this->time*1000 . " ms)"; |
477 | | $this->ping_status = $this->time*1000; |
478 | | } |
479 | | |
480 | | $this->close_socket(); |
481 | | |
482 | | return true; /* "connection refused" says: host is alive (else ping would time out) */ |
483 | | case 1: |
484 | | /* connected, so calculate the total time and return */ |
485 | | $this->time = $this->get_time($this->precision); |
486 | | |
487 | | if (($this->time*1000) <= $this->timeout) { |
488 | | $this->ping_response = "TCP Ping Success (" . $this->time*1000 . " ms)"; |
489 | | $this->ping_status = $this->time*1000; |
490 | | } |
491 | | |
492 | | $this->close_socket(); |
493 | | |
494 | | return true; |
495 | | case 0: |
496 | | /* timeout */ |
497 | | $this->ping_response = "TCP ping timed out"; |
| 468 | $w = $f = array(); |
| 469 | $r = array($this->socket); |
| 470 | $num_changed_sockets = socket_select($r, $w, $f, $to_sec, $to_usec); |
| 471 | if ($num_changed_sockets === false) { |
| 472 | $this->ping_response = "TCP ping: " . "socket_select() failed, reason: " . socket_strerror(socket_last_error()); |
| 478 | } else { |
| 479 | switch($num_changed_sockets) { |
| 480 | case 2: /* response received, so host is available */ |
| 481 | case 1: |
| 482 | /* connected, so calculate the total time and return */ |
| 483 | $this->time = $this->get_time($this->precision); |
| 484 | |
| 485 | if (($this->time*1000) <= $this->timeout) { |
| 486 | $this->ping_response = "TCP Ping Success (" . $this->time*1000 . " ms)"; |
| 487 | $this->ping_status = $this->time*1000; |
| 488 | } |
| 489 | |
| 490 | $this->close_socket(); |
| 491 | |
| 492 | return true; |
| 493 | case 0: |
| 494 | /* timeout */ |
| 495 | $this->ping_response = "TCP ping timed out"; |
| 496 | $this->ping_status = "down"; |
| 497 | |
| 498 | $this->close_socket(); |
| 499 | |
| 500 | return false; |
| 501 | } |