多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[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); } ```