想要构建聊天应用,或者甚至是游戏吗?那么,socket服务器将成为你迈出的第一步。一旦你了解了创建服务器的基本功能,那么后续的优化步骤就会变得同样简单。
socket服务器的工作方式是这样的,不间断地运行以等待客户端的连接。一旦客户端连接上了,服务器就会将它添加到客户名单中,然后开始等待来自客户端的消息。
不要走开,下面是完整的源代码:
<ol class="dp-j"><li class="alt"><span><span class="comment">// Set time limit to indefinite execution</span><span> </span></span></li><li><span>set_time_limit (<span class="number">0</span><span>); </span></span></li><li class="alt"><span> </span></li><li><span><span class="comment">// Set the ip and port we will listen on</span><span> </span></span></li><li class="alt"><span>$address = <span class="string">'localhost'</span><span>; </span></span></li><li><span>$port = <span class="number">10000</span><span>; </span></span></li><li class="alt"><span>$max_clients = <span class="number">10</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">// Array that will hold client information</span><span> </span></span></li><li><span>$client = Array(); </span></li><li class="alt"><span> </span></li><li><span><span class="comment">// Create a TCP Stream socket</span><span> </span></span></li><li class="alt"><span>$sock = socket_create(AF_INET, SOCK_STREAM, <span class="number">0</span><span>); </span></span></li><li><span><span class="comment">// Bind the socket to an address/port</span><span> </span></span></li><li class="alt"><span>socket_bind($sock, $address, $port) or die(<span class="string">'Could not bind to address'</span><span>); </span></span></li><li><span><span class="comment">// Start listening for connections</span><span> </span></span></li><li class="alt"><span>socket_listen($sock); </span></li><li><span> </span></li><li class="alt"><span>echo <span class="string">"Waiting for connections...\r\n"</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">// Loop continuously</span><span> </span></span></li><li><span><span class="keyword">while</span><span> (</span><span class="keyword">true</span><span>) { </span></span></li><li class="alt"><span><span class="comment">// Setup clients listen socket for reading</span><span> </span></span></li><li><span>$read[<span class="number">0</span><span>] = $sock; </span></span></li><li class="alt"><span><span class="keyword">for</span><span> ($i = </span><span class="number">0</span><span>; $i < $max_clients; $i++) { </span></span></li><li><span> <span class="keyword">if</span><span> (isset($client[$i][</span><span class="string">'sock'</span><span>])) </span></span></li><li class="alt"><span> $read[$i + <span class="number">1</span><span>] = $client[$i][</span><span class="string">'sock'</span><span>]; </span></span></li><li><span>} </span></li><li class="alt"><span><span class="comment">// Set up a blocking call to socket_select()</span><span> </span></span></li><li><span><span class="keyword">if</span><span> (socket_select($read, $write = NULL, $except = NULL, $tv_sec = </span><span class="number">5</span><span>) < </span><span class="number">1</span><span>) </span></span></li><li class="alt"><span> <span class="keyword">continue</span><span>; </span></span></li><li><span><span class="comment">/* if a new connection is being made add it to the client array */</span><span> </span></span></li><li class="alt"><span><span class="keyword">if</span><span> (in_array($sock, $read)) { </span></span></li><li><span> <span class="keyword">for</span><span> ($i = </span><span class="number">0</span><span>; $i < $max_clients; $i++) { </span></span></li><li class="alt"><span> <span class="keyword">if</span><span> (empty($client[$i][</span><span class="string">'sock'</span><span>])) { </span></span></li><li><span> $client[$i][<span class="string">'sock'</span><span>] = socket_accept($sock); </span></span></li><li class="alt"><span> echo <span class="string">"New client connected $i\r\n"</span><span>; </span></span></li><li><span> <span class="keyword">break</span><span>; </span></span></li><li class="alt"><span> } </span></li><li><span> elseif ($i == $max_clients - <span class="number">1</span><span>) </span></span></li><li class="alt"><span> echo <span class="string">"Too many clients...\r\n"</span><span>; </span></span></li><li><span> } </span></li><li class="alt"><span>} <span class="comment">// end if in_array</span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">// If a client is trying to write - handle it now</span><span> </span></span></li><li><span><span class="keyword">for</span><span> ($i = </span><span class="number">0</span><span>; $i < $max_clients; $i++) { </span><span class="comment">// for each client</span><span> </span></span></li><li class="alt"><span> <span class="keyword">if</span><span> (isset($client[$i][</span><span class="string">'sock'</span><span>])) { </span></span></li><li><span> <span class="keyword">if</span><span> (in_array($client[$i][</span><span class="string">'sock'</span><span>], $read)) { </span></span></li><li class="alt"><span> $input = socket_read($client[$i][<span class="string">'sock'</span><span>], </span><span class="number">1024</span><span>); </span></span></li><li><span> <span class="keyword">if</span><span> ($input == </span><span class="keyword">null</span><span>) { </span></span></li><li class="alt"><span> echo <span class="string">"Client disconnecting $i\r\n"</span><span>; </span></span></li><li><span> <span class="comment">// Zero length string meaning disconnected</span><span> </span></span></li><li class="alt"><span> unset($client[$i]); </span></li><li><span> } <span class="keyword">else</span><span> { </span></span></li><li class="alt"><span> echo <span class="string">"New input received $i\r\n"</span><span>; </span></span></li><li><span> <span class="comment">// send it to the other clients</span><span> </span></span></li><li class="alt"><span> <span class="keyword">for</span><span> ($j = </span><span class="number">0</span><span>; $j < $max_clients; $j++) { </span></span></li><li><span> <span class="keyword">if</span><span> (isset($client[$j][</span><span class="string">'sock'</span><span>]) && $j != $i) { </span></span></li><li class="alt"><span> echo <span class="string">"Writing '$input' to client $j\r\n"</span><span>; </span></span></li><li><span> socket_write($client[$j][<span class="string">'sock'</span><span>], $input, strlen($input)); </span></span></li><li class="alt"><span> } </span></li><li><span> } </span></li><li class="alt"><span> <span class="keyword">if</span><span> ($input == </span><span class="string">'exit'</span><span>) { </span></span></li><li><span> <span class="comment">// requested disconnect</span><span> </span></span></li><li class="alt"><span> socket_close($client[$i][<span class="string">'sock'</span><span>]); </span></span></li><li><span> } </span></li><li class="alt"><span> } </span></li><li><span> } <span class="keyword">else</span><span> { </span></span></li><li class="alt"><span> echo <span class="string">"Client disconnected $i\r\n"</span><span>; </span></span></li><li><span> <span class="comment">// Close the socket</span><span> </span></span></li><li class="alt"><span> socket_close($client[$i][<span class="string">'sock'</span><span>]); </span></span></li><li><span> unset($client[$i]); </span></li><li class="alt"><span> } </span></li><li><span> } </span></li><li class="alt"><span>} </span></li><li><span>} <span class="comment">// end while</span><span> </span></span></li><li class="alt"><span><span class="comment">// Close the master sockets</span><span> </span></span></li><li><span>socket_close($sock); </span></li></ol>
啊呀,乍一看这似乎是一个大工程,但是我们可以先将它分解为几个较小的部分。第一部分是创建服务器。Lines:2至20。
这部分代码设置了变量、地址、端口、最大客户端和客户端数组。接下来创建socket并将其绑定到我们指定的地址和端口上。
下面我们要做的事情就是执行一个死循环实际上我们是故意的!)。Lines:22至32。在这部分代码中我们做的第一步是设置 $read 数组。此数 组包含所有客户端的套接字和我们主服务器的套接字。这个变量稍后会用于select语句:告诉PHP监听来自这些客户端的每一条消息。
socket_select()的最后一个参数告诉我们的服务器在返回值之前最多等待5秒钟。如果它的返回值小于1,那么就表示没有收到任何数据,所以只需要返回循环顶部,继续等待。
脚本的下一个部分,是增加新的客户端到数组中。Lines:33至44。
将新的客户端放置在列表的末尾。检查以确保客户端的数量没有超过我们想要服务器处理的数量。
下面要介绍的代码块相当大,也是服务器的主要部分。当客户端将消息发送到服务器时,就需要这块代码挺身而出来处理。消息可以是各种各样的,断开消息、实际断开——只要是服务器需要处理的消息。Lines:46至末尾。
代码循环通过每个客户端并检查是否收到来自于它们的消息。如果是,获取输入的内容。根据输入来检查这是否是一个断开消息,如果是那就从数组中删除它们,反之,那它就是一个正常的消息,那我们的服务器再次通过所有客户端,并一个一个写信息给他们,跳过发送者。
好了,下面试试创造你自己的聊天服务器吧!