客户端websocket 无法连接上PHP socket问题
<br /><?php<br />error_reporting(E_ALL);<br />set_time_limit(0);<br />//ob_implicit_flush();<br /> <br />$address = '127.0.0.1';<br />$port = 100322;<br />//创建端口<br />if( ($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {<br /> echo "socket_create() failed :reason:" . socket_strerror(socket_last_error()) . "\n";<br />}<br /> <br />//绑定<br />if (socket_bind($sock, $address, $port) === false) {<br /> echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";<br />}<br /> <br />//监听<br />if (socket_listen($sock, 5) === false) {<br /> echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";<br />}<br /> <br />do {<br /> //得到一个链接<br /> if (($msgsock = socket_accept($sock)) === false) {<br /> echo "socket_accepty() failed :reason:".socket_strerror(socket_last_error($sock)) . "\n";<br /> break;<br /> }<br /> //welcome 发送到客户端<br /> $msg = "<font color='red'>server send:welcome</font><br/>";<br /> socket_write($msgsock, $msg, strlen($msg));<br /> echo 'read client message\n';<br /> $buf = socket_read($msgsock, 8192);<br /> $talkback = "received message:$buf\n";<br /> echo $talkback;<br /> if (false === socket_write($msgsock, $talkback, strlen($talkback))) {<br /> echo "socket_write() failed reason:" . socket_strerror(socket_last_error($sock)) ."\n";<br /> } else {<br /> echo 'send success';<br /> }<br /> socket_close($msgsock);<br />} while(true);<br />//关闭socket<br />socket_close($sock);<br /> <br /> <br />?><br />