Heim > Artikel > Backend-Entwicklung > php socket怎么测试
<?phperror_reporting(E_ALL);set_time_limit(0);//ob_implicit_flush();$address = '127.0.0.1';$port = 10005;//创建端口if( ($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { echo "socket_create() failed :reason:" . socket_strerror(socket_last_error()) . "\n";}//绑定if (socket_bind($sock, $address, $port) === false) { echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";}//监听if (socket_listen($sock, 5) === false) { echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";}do { //得到一个链接 if (($msgsock = socket_accept($sock)) === false) { echo "socket_accepty() failed :reason:".socket_strerror(socket_last_error($sock)) . "\n"; break; } //welcome 发送到客户端 $msg = "<font color='red'>server send:welcome</font><br/>"; socket_write($msgsock, $msg, strlen($msg)); echo 'read client message\n'; $buf = socket_read($msgsock, 8192); $talkback = "received message:$buf\n"; echo $talkback; if (false === socket_write($msgsock, $talkback, strlen($talkback))) { echo "socket_write() failed reason:" . socket_strerror(socket_last_error($sock)) ."\n"; } else { echo 'send success'; } socket_close($msgsock);} while(true);//关闭socketsocket_close($sock);?>
加入你这个文件叫 socket_test.php
在终端运行
php socket_test.php即可
socket 要在终端测试!
加入你这个文件叫 socket_test.php
在终端运行
php socket_test.php即可
换个端口试试
现在就是运行了 直接就这样了 这样就代表好了??怎么测试呢?