Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php] Pingowanie ? Jak sprawdzic dostępność serwera ?
RubiX
post
Post #1





Grupa: Zarejestrowani
Postów: 162
Pomógł: 6
Dołączył: 14.02.2007
Skąd: Poznań

Ostrzeżenie: (0%)
-----


Witam

Jestem w trakcie pisania panelu, który ma mieć funkcje włącz wyłącz serwer.
I tu pojawia sie mały problem jak sprawdzić czy dany serwer jest włączony.

Znalazłem w internecie metode php ping ale działa ona tylko do adresów ip a ja potrzebuje do adresów i i portów np:

22.22.222.22:8888

Bo mam serwery włączone na 1 adresie ip ale z roznymi portami.
Czy jest możliwość sprawdzenia tego ?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
RubiX
post
Post #2





Grupa: Zarejestrowani
Postów: 162
Pomógł: 6
Dołączył: 14.02.2007
Skąd: Poznań

Ostrzeżenie: (0%)
-----


Ok mam takie cos :
class.php
  1. <?php
  2. class SAMPQueryScript {
  3. var $ip;
  4. var $port;
  5. var $connection;
  6. var $enabled = false;
  7. var $timeout;
  8. var $serverData;
  9. var $playerData;
  10. var $error = "Unknown error";
  11.  
  12. function SAMPQueryScript($ip, $port, $timeout=1) {
  13. if (empty($ip) || !is_numeric($timeout)) return;
  14. $this->ip  = $this->verifyHost($ip);
  15. if ($this->ip === FALSE) {
  16. $this->error = "Could not resolve host name";
  17. return;
  18. }
  19. if (empty($port) || !is_numeric($port)) $port = 7777;
  20. $this->port  = $port;
  21. $this->timeout = $timeout*100;
  22. $this->enabled  = true;
  23. $this->error  = false;
  24. }
  25. function queryServer() {
  26. if (!$this->enabled) return false;
  27. if ($this->establishConnection() === FALSE) return false;
  28. $this->sendPacket("i");
  29. $this->captureServerData();
  30. if (empty($this->error)) {
  31. $this->sendPacket("c");
  32. }
  33. return true;
  34. }
  35.  
  36. function establishConnection() {
  37. $errno = $errstr = 0;
  38. $this->connection = @fsockopen("udp://".$this->ip, $this->port, $errno, $errstr, $this->timeout/1000000);
  39. if (!$this->connection) {
  40. $this->error = $errstr;
  41. return false;
  42. }
  43. else {
  44. socket_set_timeout($this->connection, 0, $this->timeout*1000);
  45. return true;
  46. }
  47. }
  48. function captureServerData() {
  49. $firstData = fread($this->connection, 4);
  50. if (empty($firstData)) {
  51. $this->error = "Brak graczy na serwie";
  52. return false;
  53. }
  54. if ($firstData != "SAMP") {
  55. $this->error = "Unidentified response";
  56. return false;
  57. }
  58. return true;
  59. }
  60. function readNextSize($sizeLength) {
  61. $nextLength = ord(fread($this->connection, $sizeLength));
  62. if ($nextLength > 0) return fread($this->connection, $nextLength);
  63. else return "";
  64. }
  65. function sendPacket($packetType="i") {
  66. // compile the packet
  67. $packet = "SAMP";
  68. $ipChunks = explode(".", $this->ip);
  69. foreach($ipChunks as $k => $v) {
  70. $packet .= chr($v);
  71. }
  72. $packet .= chr($this->port & 0xFF);
  73. $packet .= chr($this->port >> 8 & 0xFF).$packetType;
  74.  
  75. // send the packet
  76. fwrite($this->connection, $packet, strlen($packet));
  77. }
  78. function verifyHost($ip) {
  79. // verify ip address
  80. if (($longip = ip2long($ip)) !== FALSE && ($ip == long2ip($longip))) return $ip;
  81. else {
  82. $newIp = gethostbyname($ip);
  83. if ($newIp == $ip) return false;
  84. else return $newIp;
  85. }
  86. }
  87. }
  88. ?>


i 2 plik index.php:
  1. <?
  2. require "class.php";
  3.  
  4. $ip  = "87.230.77.126";  
  5. $port  = "7070";
  6.  
  7. $query = new SAMPQueryScript($ip, $port);
  8. $query->queryServer();
  9. if ($query->error) {
  10. echo "niepołączony"; 
  11. } else {
  12. echo "polaczony";
  13. }
  14. ?>


i to działa (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
Ale ma pytanie czy da sie jakos skrócić ten skrypt ? a dokladnie plik class. php ?
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 25.12.2025 - 11:19