ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
>[info] 理解stream系列函数 * **stream\_socket\_server**:创建一个服务端socket 手册地址: https://php.golaravel.com/function.stream-socket-server.html * **stream\_socket\_accept**:这个函数才是真正的建立连接 手册地址: https://php.golaravel.com/function.stream-sockete-accept.html * **stream\_socket\_client**:客户端连接服务端 手册地址: https://php.golaravel.com/function.stream-socket-client.html * **fread**:读取socket资源文件信息 手册地址: https://php.golaravel.com/function.fread.html * **fwrite**:写入socket资源文件信息 手册地址: https://php.golaravel.com/function.fwrite.html ![](https://img.kancloud.cn/ef/19/ef1984609a41aa71eeaaeee706035008_1728x677.png) >[info] 使用stream函数代码测试 * **服务端代码** ~~~ <?php echo swoole_get_local_ip()['ens33'] . ':9501' . PHP_EOL; $socket = stream_socket_server("tcp://0.0.0.0:9501", $errno, $errstr); if (!$socket) { echo "$errstr ($errno)<br />\n"; } else { while ($conn = stream_socket_accept($socket)) { fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n"); fclose($conn); } fclose($socket); } ?> ~~~ * **客户端代码** ~~~ <?php $fp = stream_socket_client("tcp://" . swoole_get_local_ip()['ens33'] . ":9501", $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { fwrite($fp, "GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n"); while (!feof($fp)) { echo fgets($fp, 1024); } fclose($fp); } ?> ~~~ * **运行效果** 1. 服务端开启监听 ![](https://img.kancloud.cn/f0/08/f0083ad581b0db98959bfc588cece707_599x190.png) 2. 客户端连接获取结果 ![](https://img.kancloud.cn/82/87/8287f6f5750b13198eb7f5b6853d9ca8_474x92.png) >[info] 使用stream 客户端 连接 tcp 服务端 * **服务端代码** ~~~ <?php echo swoole_get_local_ip()['ens33'] . ':9501' . PHP_EOL; $socket = stream_socket_server("tcp://0.0.0.0:9501", $errno, $errstr); if (!$socket) { echo "$errstr ($errno)<br />\n"; } else { while ($conn = stream_socket_accept($socket)) { fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n"); fclose($conn); } fclose($socket); } ?> ~~~ * **客户端代码** ~~~ <?php $client = new Swoole\Client(SWOOLE_SOCK_TCP); if (!$client->connect(swoole_get_local_ip()['ens33'], 9501, -1)) { exit("connect failed. Error: {$client->errCode}\n"); } $client->send("hello world\n"); echo $client->recv(); $client->close(); ~~~ * **运行效果** 1. 服务端开启监听 ![](https://img.kancloud.cn/84/15/8415043531efe837e0dbbf1569302372_661x175.png) 2. 客户端连接获取结果 ![](https://img.kancloud.cn/07/ae/07aed0ba4af6107d61e56abd15523749_643x201.png) >[info] 使用ctp客户端 连接 stream 服务端 * **服务端代码** ~~~ <?php echo swoole_get_local_ip()['ens33'] . ':9501' . PHP_EOL; $server = new Swoole\Server("0.0.0.0", 9501); $server->on('connect', function ($server, $fd){ echo "connection open: {$fd}\n"; }); $server->on('receive', function ($server, $fd, $reactor_id, $data) { $server->send($fd, "Swoole: {$data}"); $server->close($fd); }); $server->on('close', function ($server, $fd) { echo "connection close: {$fd}\n"; }); $server->start(); ~~~ * **客户端代码** ~~~ <?php $fp = stream_socket_client("tcp://" . swoole_get_local_ip()['ens33'] . ":9501", $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { fwrite($fp, "GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n"); while (!feof($fp)) { echo fgets($fp, 1024); } fclose($fp); } ?> ~~~ * **运行效果** 1. 服务端开启监听 ![](https://img.kancloud.cn/45/ff/45ff8f83452abcf1ddfba9f30ca9d54b_495x130.png) 2. 客户端连接获取结果 ![](https://img.kancloud.cn/44/11/4411ae4bccb321838c04f8a56fadfc41_588x225.png) >[danger] **由此可见**:只要协议相同,客户端 或 服务端 可以是其他物理机 或其他 c++ java python go 等。