利用PHP如何實現(xiàn)Socket服務器
socket服務器的工作方式是這樣的,不間斷地運行以等待客戶端的連接。一旦客戶端連接上了,服務器就會將它添加到客戶名單中,然后開始等待來自客戶端的消息。
不要走開,下面是完整的源代碼:
// Set time limit to indefinite execution set_time_limit (0); // Set the ip and port we will listen on $address = 'localhost'; $port = 10000; $max_clients = 10; // Array that will hold client information $client = Array(); // Create a TCP Stream socket $sock = socket_create(AF_INET, SOCK_STREAM, 0); // Bind the socket to an address/port socket_bind($sock, $address, $port) or die('Could not bind to address'); // Start listening for connections socket_listen($sock); echo "Waiting for connections...\r\n"; // Loop continuously while (true) { // Setup clients listen socket for reading $read[0] = $sock; for ($i = 0; $i < $max_clients; $i++) { if (isset($client[$i]['sock'])) $read[$i + 1] = $client[$i]['sock']; } // Set up a blocking call to socket_select() if (socket_select($read, $write = NULL, $except = NULL, $tv_sec = 5) < 1) continue; /* if a new connection is being made add it to the client array */ if (in_array($sock, $read)) { for ($i = 0; $i < $max_clients; $i++) { if (empty($client[$i]['sock'])) { $client[$i]['sock'] = socket_accept($sock); echo "New client connected $i\r\n"; break; } elseif ($i == $max_clients - 1) echo "Too many clients...\r\n"; } } // end if in_array // If a client is trying to write - handle it now for ($i = 0; $i < $max_clients; $i++) { // for each client if (isset($client[$i]['sock'])) { if (in_array($client[$i]['sock'], $read)) { $input = socket_read($client[$i]['sock'], 1024); if ($input == null) { echo "Client disconnecting $i\r\n"; // Zero length string meaning disconnected unset($client[$i]); } else { echo "New input received $i\r\n"; // send it to the other clients for ($j = 0; $j < $max_clients; $j++) { if (isset($client[$j]['sock']) && $j != $i) { echo "Writing '$input' to client $j\r\n"; socket_write($client[$j]['sock'], $input, strlen($input)); } } if ($input == 'exit') { // requested disconnect socket_close($client[$i]['sock']); } } } else { echo "Client disconnected $i\r\n"; // Close the socket socket_close($client[$i]['sock']); unset($client[$i]); } } } } // end while // Close the master sockets socket_close($sock);
哎呀,乍一看這似乎是一個大工程,但是我們可以先將它分解為幾個較小的部分。
第一部分是創(chuàng)建服務器。Lines:2至20。
這部分代碼設置了變量、地址、端口、最大客戶端和客戶端數(shù)組。接下來創(chuàng)建socket并將其綁定到我們指定的地址和端口上。
下面我們要做的事情就是執(zhí)行一個死循環(huán)(實際上我們是故意的!)。Lines:22至32。
在這部分代碼中我們做的第一步是設置 $read 數(shù)組。此數(shù) 組包含所有客戶端的套接字和我們主服務器的套接字。這個變量稍后會用于select語句:告訴PHP監(jiān)聽來自這些客戶端的每一條消息。
socket_select()的最后一個參數(shù)告訴我們的服務器在返回值之前最多等待5秒鐘。如果它的返回值小于1,那么就表示沒有收到任何數(shù)據(jù),所以只需要返回循環(huán)頂部,繼續(xù)等待。
腳本的下一個部分,是增加新的客戶端到數(shù)組中。Lines:33至44。
將新的客戶端放置在列表的末尾。檢查以確??蛻舳说臄?shù)量沒有超過我們想要服務器處理的數(shù)量。
下面要介紹的代碼塊相當大,也是服務器的主要部分。當客戶端將消息發(fā)送到服務器時,就需要這塊代碼挺身而出來處理。消息可以是各種各樣的,斷開消息、實際斷開——只要是服務器需要處理的消息。Lines:46至末尾。
代碼循環(huán)通過每個客戶端并檢查是否收到來自于它們的消息。如果是,獲取輸入的內(nèi)容。根據(jù)輸入來檢查這是否是一個斷開消息,如果是那就從數(shù)組中刪除它們,反之,那它就是一個正常的消息,那我們的服務器再次通過所有客戶端,并一個一個寫信息給他們,跳過發(fā)送者。
好了,下面試試創(chuàng)造你自己的聊天服務器吧!
相關文章
在smarty中調(diào)用php內(nèi)置函數(shù)的方法
在smarty中調(diào)用php的內(nèi)置函數(shù),相信很多人都不是很很了解smarty的一個重要功能,就是能在smarty模板里面調(diào)用php的內(nèi)置函數(shù)2013-02-02php 靜態(tài)頁面中顯示動態(tài)內(nèi)容
靜態(tài)頁面中顯示動態(tài)內(nèi)容,一些網(wǎng)站的qq在線狀態(tài),還有購物車用的是這個方法2009-08-08PHP實現(xiàn)網(wǎng)絡請求的方法總結(jié)
對于php發(fā)送網(wǎng)絡請求,我們最常用的請求就是curl,有時我們也會用到file_get_contents函數(shù)發(fā)送網(wǎng)絡請求。本文為大家總結(jié)了php實現(xiàn)網(wǎng)絡請求的方法及函數(shù),感興趣的可以學習一下2022-06-06解析PHP中DIRECTORY_SEPARATOR,PATH_SEPARATOR兩個常量的作用
本篇文章是對PHP中DIRECTORY_SEPARATOR,PATH_SEPARATOR兩個常量的作用進行了詳細的分析介紹,需要的朋友參考下2013-06-06PHP微信開發(fā)之微信錄音臨時轉(zhuǎn)永久存儲
這篇文章主要為大家詳細介紹了PHP微信開發(fā)之微信錄音臨時轉(zhuǎn)永久存儲,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01PHP與MySQL開發(fā)中頁面亂碼的產(chǎn)生與解決
一般來說,亂碼的出現(xiàn)有2種原因,首先是由于編碼(charset)設置錯誤,導致瀏覽器以錯誤的編碼來解析,從而出現(xiàn)了滿屏亂七八糟的“天書”,其次是文件被以錯誤的編碼打開,然后保存,比如一個文本文件原先是GB2312編碼的,卻以UTF-8編碼打開再保存。要解決上述亂碼問題,首先需要知道開發(fā)中哪些環(huán)節(jié)涉及到了編碼:2008-03-03