Home > Article > Backend Development > 瞥socket 好多东西不明白
看socket 好多东西不明白
网上下了个html5 发socket 给php.server
php.server里面有些地方不明白呀
部分代码
function __construct ($address, $port) {<br /> error_reporting (E_ALL);<br /> set_time_limit (0);<br /> ob_implicit_flush ();<br /> <br /> // Socket creation<br /> $this->master = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) or die("socket_create() failed");<br /> socket_set_option ($this->master, SOL_SOCKET, SO_REUSEADDR, 1) or die("socket_option() failed");<br /> socket_bind ($this->master, $address, $port) or die("socket_bind() failed");<br /> socket_listen ($this->master, 20) or die("socket_listen() failed");<br /> $this->sockets[] = $this->master;<br /> $this->say ("Server Started : " . date ('Y-m-d H:i:s'));<br /> $this->say ("Listening on : {$address} {$port}");<br /> $this->say ("Master socket : {$this->master}\n");<br /> <br /> // Main loop<br /> while (true) {<br /> <br /> $changed = $this->sockets;<br /> socket_select ($changed, $write = NULL, $except = NULL, NULL);<br /> <br /> foreach ($changed as $socket) {<br /> if ($socket == $this->master) {<br /> <br /> <br /> $client = socket_accept ($this->master);<br /> <br /> if ($client < 0) {<br /> $this->log ("socket_accept() failed");<br /> continue;<br /> }<br /> else {<br /> // Connects the socket<br /> $this->connect ($client);<br /> }<br /> }<br /> else {<br /> <br /> $bytes = @socket_recv ($socket, $buffer, 2048, 0);<br /> if ($bytes == 0) {<br /> // On socket.close ();<br /> $this->disconnect ($socket);<br /> }<br /> else {<br /> // Retrieve the user from his socket<br /> $user = $this->getuserbysocket ($socket);<br /> <br /> if (!$user->handshake) {<br /> $this->dohandshake ($user, $buffer);<br /> }<br /> else {<br /> $this->process ($user, $this->decode ($buffer));<br /> }<br /> }<br /> }<br /> }<br /> }<br /> }