Ping.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: chendeben
  5. * Date: 2018/8/31
  6. * Time: 下午9:20
  7. */
  8. namespace PhpLife\App\Api\Controller;
  9. use PhpLife\App\Api\Controller;
  10. use PhpLife\App\Api\Package\Database\Server;
  11. class Ping extends Controller
  12. {
  13. /**
  14. * 执行逻辑
  15. * @return mixed
  16. */
  17. protected function main()
  18. {
  19. $data=Server::select();
  20. foreach ($data as $val) {
  21. $ip=$val['ip'];
  22. $port=$val['port'];
  23. $status=$this->check($ip, $port);
  24. if ($val['status']!=$status){
  25. Server::update(['status' => $status], ['id'=>$val['id']]);
  26. }
  27. }
  28. }
  29. /**
  30. * @param string $ip ip
  31. * @param int $port 端口
  32. * @return int 返回状态
  33. */
  34. public function check($ip, $port)
  35. {
  36. $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  37. socket_set_nonblock($sock);
  38. socket_connect($sock, $ip, $port);
  39. socket_set_block($sock);
  40. $status = socket_select($r = array($sock), $w = array($sock), $f = array($sock), 2);
  41. return $status;
  42. }
  43. }