dimanche 26 juin 2016

How to inform client that data has finished without closing socket [duplicate]

This question already has an answer here:

I'm having trouble writing a server-client communication passing a bitmap as data.

At my server side I have

InputStream in = rs.getBinaryStream(1);
                        ByteArrayOutputStream f2 = new ByteArrayOutputStream();
                        int c = 0;
                        while ((c = in.read()) > -1) {
                            f2.write(c);
                        }
                        bytes = f2.toByteArray();
                        username2.write(bytes, 0, bytes.length);

And at my Android client, I have

 ByteArrayOutputStream out = new ByteArrayOutputStream();
            final byte[] buffer = new byte[2048];
            int read = -1;

            while ((read = receiveimagem.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }

The problem is, the client just understands that the sending is over when I close the outputStream at server side. But the structure is inside a loop and if I close the socket, it won't work again. How can I tell the client that the data is over? The code stucks in the Android client at the while. I know I should send the length and check it, but I don't know how to use the length to check the end of the file.

Aucun commentaire:

Enregistrer un commentaire