PHP socket实现 readInt 和 readString 以及和Java端对接
原创
©著作权归作者所有:来自51CTO博客作者TechOnly的原创作品,请联系作者获取转载授权,否则将追究法律责任
java:
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
out.writeInt("REWQTY".getBytes().length);
out.write("REWQTY".getBytes());
out.close();//php端是阻塞的,除非close
php:
function readInt($sock)
$buf = socket_read($sock,4);
if ($buf){
return unpack('N',$buf)[1];
}else{
throw new Exception();
}
}
function readString($socket)
$n = readInt($socket);
return socket_read($socket, $n);
}
echo readString($sock,$l);
echo "\n";