ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## 函数 ``` stream_​socket_​accept stream_​socket_​client stream_​socket_​enable_​crypto stream_​socket_​get_​name 获取本地或者远程的套接字名称 stream_​socket_​pair stream_​socket_​recvfrom stream_​socket_​sendto stream_​socket_​server stream_​socket_​shutdown stream_set_blocking 为资源流设置阻塞或者阻塞模式 stream_set_timeout 设置超时时间 ``` ## 实例 ## 创建socket 服务 ``` $socket = stream_socket_server("0.0.0.0:9090",$errno,$errstr); if (!$socket){ echo "$errstr ($errno)<br />\n"; die(); } while($client = stream_socket_accept($socket,60)){ print_r(stream_socket_get_name($client,true)); // 也可使用 fread 与fwrtie while ($msg = @stream_socket_recvfrom($client,1024)){ print_r($msg."\n"); stream_socket_sendto($client,"to:".$msg); fclose($client); } } ``` ### 模拟socket客户端 ``` $fp = stream_socket_client("tcp://localhost:8010", $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { fwrite($fp, "GET / HTTP/1.0\r\nHost: localhost\r\nAccept: */*\r\n\r\n"); while (!feof($fp)) { echo fgets($fp, 1024); } fclose($fp); } ```