Home >Backend Development >PHP Tutorial >php socket 如何实现一个标准的writeInt

php socket 如何实现一个标准的writeInt

WBOY
WBOYOriginal
2016-06-06 20:14:301057browse

就是像java里一样。
主要我的目的是和java的对接
java这边要read:

<code>DataInputStream stream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
int l =  stream.readInt();
</code>

另外求下writeString或writeUTF
最后麻烦下,java端如果用 stream.readFully ,PHP端怎么办

回复内容:

就是像java里一样。
主要我的目的是和java的对接
java这边要read:

<code>DataInputStream stream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
int l =  stream.readInt();
</code>

另外求下writeString或writeUTF
最后麻烦下,java端如果用 stream.readFully ,PHP端怎么办

先使用pack将需要传输的转换为二进制格式,在通过socket_write发送即可。

可以简单模拟成

<code>function writeInt($socket, $i)
{
    $data = pack('N', $i); // N 表示32位大端字节序
    socket_write($socket, $data);
}</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn