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";