2012-07-24 88 views
0

下面的代碼是我PHP CLI TCP服務器套接字,但我儘量讓TCP & UDP套接字服務器或只是UDP服務器,但我不知道如何使它PHP CLI UDP服務器套接字

MyCodes:

// PHP SOCKET SERVER 
error_reporting(E_ERROR); 
// Configuration variables 
$host = "192.168.0.1"; 
$port = 5009; 
$max = 20; 

// MySQL Database Conf 
$db_host ='localhost'; 
$db_user ='root'; 
$db_pass ='5222348'; 
$db_database ='gps'; 

mysql_connect("$db_host", "$db_user", "$db_pass")or die("################# Can't connet to mysql server"); 
mysql_select_db("$db_database")or die("################# Can't connet to database"); 

// -------------------------------- 
$client = array(); 

// No timeouts, flush content immediatly 
set_time_limit(0); 
ob_implicit_flush(); 

// Server functions 
function rLog($msg){ 
      $msg = "\n[".date('Y-m-d H:i:s')."]:".$msg; 
      echo($msg); 

} 
// Create socket 
// $sock = socket_create(AF_INET,SOCK_DGRAM,0) or die("[".date('Y-m-d H:i:s')."] Could not create socket\n"); UDP communication 
$sock = socket_create(AF_INET,SOCK_STREAM,0) or die("[".date('Y-m-d H:i:s')."] Could not create socket\n"); 
// Bind to socket 
socket_bind($sock,$host,$port) or die("[".date('Y-m-d H:i:s')."] Could not bind to socket\n"); 
// Start listening 
socket_listen($sock) or die("[".date('Y-m-d H:i:s')."] Could not set up socket listener\n"); 

rLog(" 
Server started at ".$host.":".$port."\nrev:23-7-11:13"); 
// Server loop 
while(true){ 
      socket_set_block($sock); 
      // Setup clients listen socket for reading 
      $read[0] = $sock; 
      for($i = 0;$i<$max;$i++){ 
          if($client[$i]['sock'] != null) 
             $read[$i+1] = $client[$i]['sock']; 
      } 
      // Set up a blocking call to socket_select() 
      $ready = socket_select($read,$write = NULL, $except = NULL, $tv_sec = NULL); 
      // If a new connection is being made add it to the clients array 
      if(in_array($sock,$read)){ 
          for($i = 0;$i<$max;$i++){ 
             if($client[$i]['sock']==null){ 
                if(($client[$i]['sock'] = socket_accept($sock))<0){ 
                   rLog("socket_accept() failed: ".socket_strerror($client[$i]['sock'])); 
                }else{ 
                   socket_getpeername($client[$i]['sock'], $address, $port); 
                   rLog("Client #".$i." connected from : ".$address.":".$port.""); 
                } 
                break; 
             }elseif($i == $max - 1){ 
                rLog("Too many clients"); 
             } 
          } 
          if(--$ready <= 0) 
          continue; 
      } 
      for($i=0;$i<$max;$i++){ 
          if(in_array($client[$i]['sock'],$read)){ 
             $input = socket_read($client[$i]['sock'],1024); 
             if($input==null){ 
                unset($client[$i]); 
             } 
             $n = trim($input); 
             $com = split(" ",$n); 
             if($n=="EXIT"){ 
                if($client[$i]['sock']!=null){ 
                   // Disconnect requested 
                   socket_close($client[$i]['sock']); 
                   unset($client[$i]['sock']); 
                   rLog("Disconnected(2) client #".$i); 
                   for($p=0;$p<count($client);$p++){ 
                       socket_write($client[$p]['sock'],"DISC ".$i.chr(0)); 
                   } 
                   if($i == $adm){ 
                       $adm = -1; 
                   } 
                } 
             }elseif($n=="TERM"){ 
                // Server termination requested 
                socket_close($sock); 
                rLog("Terminated server (requested by client #".$i.")"); 
                exit(); 
             }elseif($input){ 


                // ----------------------------------------- // 
                // $handle = fopen('inbox.txt', 'a+'); 
                // fwrite($handle, 
                 // $input."\n". 
                 // "------------------------------------------------------------\n"); 
                // ########################################################################################### // 
                // $input = "$POS,115316,160137.000,A,2719.9343,N,05310.5553,E,0.00,,180712,,,A/00000,00000/0,0,0,0/794"; // Sample Data 
                $input_first = explode(",", $input); 

                $frstvalue=$input_first[0]; // $POS   - Command 

                echo $input; 
                  $handle = fopen('force.log', 'a+'); 
                  fwrite($handle, 
                   "Time:[".date('Y-m-d H:i:s')."] , from : ".$address.":".$port."\ninput: ".$input."\n". 
                   "-------------------------------------------------------------------------------------\n"); 
                ########################### 
             } 
          }else{ 

             //} 
          } 
      } 
} 
// Close the master sockets 
socket_close($sock); 

上面的代碼是完全在TCP連接上的工作,但不能給UDP請求s。

回答

0

UDP,是一個數據報傳輸,是由TCP有點不同:

  • 刪除調用socket_listen,它並不適用於UDP。
  • 取消與socket_accept相同的呼叫。
  • 使用socket_recvfrom在一個循環中接收$sock的數據報。

也就是說,您的原始$sock是數據套接字,即您在這裏只需要一個。

PHP還提供了簡化的stream_socket_server界面,您可以使用它與"udp://..." URL。

+0

謝謝,我這樣做,但認爲我設置此更改有問題,可以設置上面的第一個職位原始代碼的變化? ;) – Root125 2012-07-24 12:28:10

+0

我真的不明白你的意思。如果你想讓我編輯你的代碼 - 不,我沒有那樣做。 – 2012-07-24 12:35:23

+0

我使用它,但給錯誤: - ? ,從udp發送輸入不是服務器上的回顯。 – Root125 2012-07-24 12:41:17