Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]PHP -> Socket długie łączenie gdy serwer nie odpowiada
Forum PHP.pl > Forum > Przedszkole
savander
Witam, mam taki problem z kodem PHP, otóż napisałem sobie status serwerów do pewnej gry, status działa jeżeli wszystkie serwery są online, jednak jeżeli jakikolwiek z nich by padł to strona ładuje się w nieskończoność.
Jak mogę temu zapobiec?

  1. <?php $ip = array(
  2. array('46.4.xx.xx',8328),
  3. array('46.4.xx.xx',8329),
  4. array('46.4.xx.xx'',8330),
  5. array('46.4.xx.xx',8315),
  6. );
  7. ?>
  8. <DOCTYPE html>
  9. <html>
  10. <head>
  11. <title>DDracemaX Status</title>
  12. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
  13. <style type="text/css">
  14. body {
  15. background-color: White;
  16. font-family:Verdana, Geneva, sans-serif;
  17. }
  18. #status{
  19. width: 740px;
  20. background-color: #4A4F7A;
  21. padding: 30px;
  22. border-bottom-left-radius: 15px;
  23. border-bottom-right-radius: 15px;
  24. color:#18CDCA;
  25. font-size: 28px;
  26. font-weight: bold;
  27. margin-bottom: 20px;
  28. }
  29. caption{
  30. border-top-left-radius: 15px;
  31. border-top-right-radius: 15px;
  32. font-weight: bold;
  33. color: White;
  34. padding: 10px;
  35. background-color:#343857;
  36. }
  37. table{
  38. border-spacing:5em
  39. width:800px;
  40. background-color:#292C44;
  41.  
  42. }
  43. table th{
  44. color: #FF610F ;
  45. }
  46. table td{
  47. color: #F0F0F1;
  48. }
  49. #container{
  50. display:block;
  51. width:800px;
  52. margin:0 auto;
  53. margin-top: -20px;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <div id="container">
  59.  
  60.  
  61. <?php
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. function serverConnect($ip,$port){
  69. $socket = @stream_socket_client("udp://$ip:$port", $errno, $errstr,0);
  70. if($socket){
  71. fwrite($socket, "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x67\x69\x65\x33\x05");
  72. $response = fread($socket, 2048);
  73.  
  74. if($response){
  75. $serwer = explode("\x00", $response);
  76. $name = $serwer[2];
  77. $maxplayers = $serwer[9];
  78. $pingame = $serwer[8];
  79. $map = $serwer[3];
  80. if($pingame >= 1){
  81. echo '<b>'.$name.'['.$pingame.'/'.$maxplayers.']'.' Map: '.$map.'</b></br><span style="color: #787878;font-size: 15px;">IP:'.$ip.':'.$port.'</span>';
  82. }
  83. }
  84. }
  85. }
  86.  
  87.  
  88.  
  89. function getPlayers($ip, $port){
  90. $socket = @stream_socket_client("udp://$ip:$port", $errno, $errstr,0);
  91. $specs = array();
  92. $ingames = array();
  93.  
  94. if($socket){
  95. fwrite($socket, "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x67\x69\x65\x33\x05");
  96. $response = fread($socket, 2048);
  97.  
  98. if($response){
  99. $serwer = explode("\x00", $response);
  100. $pingame = $serwer[8];
  101. $serwerCount = count($serwer);
  102. if($serwer[10] != null){
  103. for($i=10; $i < $serwerCount; $i+=5){
  104. $team = $serwer[$i+4];
  105. if($team != null){
  106. if($team == 1){
  107. $ingames[] = array(htmlentities($serwer[$i], ENT_QUOTES, "UTF-8"),
  108. $serwer[$i+3],
  109. htmlentities($serwer[$i+1], ENT_QUOTES, "UTF-8"));
  110.  
  111. }else{
  112. $specs[] = array(htmlentities($serwer[$i], ENT_QUOTES, "UTF-8"),
  113. htmlentities($serwer[$i+1], ENT_QUOTES, "UTF-8"));
  114. }
  115. }else;
  116. }
  117. }
  118. }
  119. if($pingame >= 1){
  120. echo '<div id="table">
  121. <div id="players" style="float:left;">
  122. <table width="800px" align="left" cellpadding="5" cellspacing="0">
  123. <caption align="top"><span style="margin-right: 380px">Players</span> Spectators</caption>
  124. <tr style="background-color:#434871;">
  125. <th width="100px">Score</th> <th width="150px">Player</th> <th width="150px">Clan</th><th width="200px">Player</th> <th width="200px">Clan</th>
  126. </tr>';
  127.  
  128. for($i = 0; $i <= count($ingames); $i++){
  129. if(($i % 2) == 1){
  130. echo '<tr><td align="center" style="background-color:#343858;">'.$ingames[$i][1].'</td><td align="center" style="background-color:#343858;">'.$ingames[$i][0].'</td><td align="center" style="background-color:#343858;">'.$ingames[$i][2].'</td><td align="center" style="background-color:#404672;">'.$specs[$i][0].'</td> <td align="center" style="background-color:#404672;">'.$specs[$i][1].'</td></tr>';
  131. }else{
  132. echo '<tr><td align="center">'.$ingames[$i][1].'</td><td align="center">'.$ingames[$i][0].'</td><td align="center">'.$ingames[$i][2].'</td><td align="center" style="background-color:#515890;">'.$specs[$i][0].'</td> <td align="center" style="background-color:#515890;">'.$specs[$i][1].'</td></tr>';
  133. }
  134.  
  135. }
  136.  
  137. echo '
  138. </table>
  139. </div>
  140. ';
  141. // for($i = 0; $i <= count($specs); $i++){
  142. // echo '<tr></tr>';
  143. //}
  144.  
  145. echo '
  146. </div><div style="clear:both;maring-top: 40px;"></div></br></br>';
  147. }
  148.  
  149. }
  150.  
  151. }
  152.  
  153. function countPlayers(){
  154. global $ip;
  155. $pingame = array();
  156. for($i = 0; $i <= count($ip); $i++){
  157. $socket = @stream_socket_client("udp://".$ip[$i][0].":".$ip[$i][1], $errno, $errstr, 0);
  158.  
  159. if($socket){
  160. fwrite($socket, "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x67\x69\x65\x33\x05");
  161. $response = fread($socket, 2048);
  162.  
  163. if($response){
  164. $serwer = explode("\x00", $response);
  165. $pingame[] = $serwer[8];
  166. }
  167. }
  168. }
  169. for($i = 0; $i <= count($pingame); $i++){
  170. $ilosc = $ilosc + $pingame[$i];
  171. }
  172.  
  173. return $ilosc;
  174. }
  175.  
  176.  
  177.  
  178.  
  179. $count = (countPlayers() >= 1) ? "players" : "player";
  180. echo "<div id=\"status\">Status: ".countPlayers()." ".$count."</br> <span style=\"float:right; font-size: 16px;color: white;\">Empty servers are not shown</span></div>";
  181. for($i = 0; $i <= count($ip); $i++){
  182. serverConnect($ip[$i][0],$ip[$i][1]);
  183. getPlayers($ip[$i][0],$ip[$i][1]);
  184. }
  185.  
  186. ?>
  187. </div>
  188. <body>
  189. </html>
  190.  
  191.  


Wiecie jak to rozwiązać? Ew. możliwe ze ja popełniłem jakiś błąd
Pyton_000
Swego czasu pisałem metodkę do wyszukiwania najszybszych proxy. Może to Ci pomoże
  1. private function __getProxy() {
  2. $proxy = array(
  3. '189.112.3.87:3128',
  4. '189.112.3.86:3128',
  5. '216.218.133.217:80',
  6. '85.114.141.191:80',
  7. '211.155.81.99:80',
  8. '61.144.14.99:80',
  9. '118.195.65.247:80',
  10. '184.169.162.184:80',
  11. '217.74.174.243:3128',
  12. '103.31.0.245:8080'
  13. );
  14.  
  15. $checkedProxy = array();
  16. foreach ($proxy as &$val) {
  17. echo "Proxy: {$val} - ";
  18. $val = explode(':', $val);
  19.  
  20. $time_start = microtime(true);
  21. $fp = @fsockopen($val[0], $val[1], $errno, $errstr, 5);
  22. $time_end = microtime(true);
  23. $time = $time_end - $time_start;
  24. echo "time: {$time}\n";
  25.  
  26. if(!$fp) {
  27. unset($val);
  28. continue;
  29. }
  30. $checkedProxy[round($time, 5)*1000000] = implode($val, ':');
  31.  
  32. }
  33. ksort($checkedProxy);
  34.  
  35. $checkedProxy = array_values($checkedProxy);
  36.  
  37. return $checkedProxy;
  38. }
savander
Nie wiem co może mi pomóc z twojego kodu, ponieważ mój skrypt ogólnie jak serwery są online to strona się ładuje i pokazuje wszystkie dane te co chce, jednakże jak jakikolwiek serwer z listy padnie, to strona nie chce się załadować, tak jak by cały czas próbowała się łączyć do tego jednego serwera ( który nie żyje) i nie wiem jak to naprawić :/
Pyton_000
  1. @stream_socket_client("udp://".$ip[$i][0].":".$ip[$i][1], $errno, $errstr, 0);

0 zmień na 1 lub mniej i analogicznie pozostałe funkcje.
savander
Zmieniłem na 1, niestety specjalnie wpisałem jedno błędne ip, teoretycznie powinien pójść timeout, a następnie iść do następnego adresu, jednakże strona po prostu umiera. Nie ładuje się.

Zdaje się, że nikt nie umie pomóc, szkoda :/

Chyba że znacie ew. inne rozwiązanie ? Abym mógł pobrać dane po danym pakiecie?smile.gif
johny_s
Może spróbuj z curlem
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.