[英]How to read from byte stream in Java (PHP/Java socket communication)
So. 所以。 I have a PHP socket server and Java socket client.
我有一个PHP套接字服务器和Java套接字客户端。
Maybe it's a stupid question... but i didn't find an answer. 也许这是一个愚蠢的问题...但是我没有找到答案。
In client, i need to read incoming bytes from input stream and put them into byte array. 在客户端中,我需要从输入流中读取传入的字节并将其放入字节数组。
I tried to do so: 我试图这样做:
[CLIENT (Java)] [客户(Java)]
public static byte[] read(BufferedInputStream in)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[32768];
while (true) {
int readBytesCount = in.read(buffer);
if (readBytesCount == -1) {
break;
}
if (readBytesCount > 0) {
baos.write(buffer, 0, readBytesCount);
}
}
baos.flush();
baos.close();
return baos.toByteArray();
}
[SERVER (PHP)] [服务器(PHP)]
function write(&$client, $message)
{
$message = explode("\n", $message);
foreach ($message as $line)
{
socket_write($client['sock'], $line."\0");
}
}
But when it read all bytes, in.read() doesn't return -1, so the cycle doesn't stop. 但是,当它读取所有字节时,in.read()不会返回-1,因此循环不会停止。 One time it returns 13 (length) and then - nothing.
一次返回13(长度),然后-没有任何内容。 Any ideas?
有任何想法吗?
SOLVED! 解决了!
[CLIENT (Java)] [客户(Java)]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[32768];
while (true) {
int readBytesCount = in.read(buffer);
if (getString(buffer).contains("#!EOS!#")) {
baos.flush();
baos.close();
return baos.toByteArray();
}
if (readBytesCount > 0) {
baos.write(buffer, 0, readBytesCount - 1);
}
}
[SERVER (PHP)] [服务器(PHP)]
function write(&$client, $message)
{
$message = explode("\n", $message);
$message = str_replace("\r", "", $message);
foreach ($message as $line)
{
rLog("Write to ".$client['ip'].": ".$line);
socket_write($client['sock'], $line."\0") or die("[".date('Y-m-d H:i:s')."] Could not write to socket\n");
}
socket_write($client['sock'], "#!EOS!#");
}
If your socket is still opened I believe that read will never retun -1. 如果您的套接字仍然打开,我相信读操作将永远不会恢复-1。 You do not close the trasmission ( in terms of file an EOF is not reached ).
您不关闭传输(就文件而言,未达到EOF)。 I believe that your read call returns 0 when your message is end ( instead of -1 ).
我相信您的消息结束时,您的read调用返回0(而不是-1)。
Java docs: Read returns: * The specified number of bytes have been read, * The read method of the underlying stream returns -1, indicating end-of-file, or * The available method of the underlying stream returns zero, indicating that further input requests would block. Java文档:读取返回:*已读取指定的字节数,*基础流的read方法返回-1,表示文件结束,或者*基础流的可用方法返回零,表示进一步输入请求将被阻止。
So I believe you need to send a terminating character sequence, or the number of characters you will send, in order to identify on client-side the boundaries of your chunk of data ( the message ) inside the streaming of data. 因此,我认为您需要发送一个终止字符序列或要发送的字符数,以便在客户端识别出数据流中数据块(消息)的边界。
I agree with Stefano. 我同意斯特凡诺。 No EOF or terminate signal is happening on the php server side.
php服务器端未发生EOF或终止信号。 socket_write is just writing $message to the client side.
socket_write只是将$ message写入客户端。 If your intent is to terminate the connection after the entire message has been written, then you might have to explicitly use socket_shutdown/socket_close.
如果您的意图是在写完整个消息后终止连接,则可能必须显式使用socket_shutdown / socket_close。
You can also try socket_send with flags. 您也可以尝试使用带有标志的socket_send。 Here is the doc for that http://www.php.net/manual/en/function.socket-send.php
这是该http://www.php.net/manual/zh/function.socket-send.php的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.