php socket 读取缓存区域
<?php //创建socket套接字 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); //设置阻塞模式 socket_set_block($socket); //为套接字绑定ip和端口 socket_bind($socket,'127.0.0.1',3046); //监听socket socket_listen($socket,4); while(true) { //接收客户端请求 if(($msgsocket = socket_accept($socket)) !== false) { //读取请求内容 $buf = socket_read($msgsocket, 2); echo "Received msg: $buf "; $buf2 = socket_read($msgsocket, 2); echo "Received msg: $buf2 "; $str = "this is a service message"; //向连接的客户端发送数据 socket_write($msgsocket, $str,strlen($str)); //操作完之后需要关闭该连接否则 feof() 函数无法正确识别打开的句柄是否读取完成 socket_close($msgsocket); } }
socket_read 第二个参数来决定要从缓冲区最多读取多少字符 上面例子是2个个读取的
$buf = socket_read($msgsocket, 8192);
这个代表最多8192个字符可以读取 如果发过来100个字符 那就按照100个字符读取