简体   繁体   中英

How to read from byte stream in Java (PHP/Java socket communication)

So. I have a PHP socket server and Java socket client.
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)]

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)]

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. One time it returns 13 (length) and then - nothing. Any ideas?

SOLVED!
[CLIENT (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)]

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. You do not close the trasmission ( in terms of file an EOF is not reached ). I believe that your read call returns 0 when your message is end ( instead of -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.

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. socket_write is just writing $message to the client side. 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.

You can also try socket_send with flags. Here is the doc for that http://www.php.net/manual/en/function.socket-send.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM