0

This is what you usually do when sending text data

// Receiver code
while (mRun && (response = in.readLine()) != null && socket.isConnected()) {
    // Do stuff                 
}

// Sender code
printWriter.println(mMessage);
printWriter.flush();

but when working with DataOutputStream#write(byte[]) to send byte[], how do you write a while loop to receive sent data.

All I have found is this, but it doesn't loop, so I'm guessing this will just run on the first sent message:

int length = in.readInt();
byte[] data = new byte[length];
in.readFully(data);

How can I achieve this?

PS: yep, I'm new to socket programming.

EDIT: I'm sending a byte array each 3 to 5 seconds. This is what I've got so far.

// In the client side, in order to send byte[]. This is executed each 3 seconds.
if(out != null) {
    try {
        out.writeInt(encrypted.length);
        out.write(encrypted);
        out.writeInt(0);
        out.flush();
        return true;

    } catch (IOException e) {
        e.printStackTrace();

        return false;
    }
}


 // In the serverside, in order to receive byte[] sent from client (also executed 3 to 5 seconds due to bytes being sent at said rate.  "client" being the Socket instance.
while(true && client.isConnected()) {

    byte[] data = null;

    while(true) {
        int length = in.readInt();
        if(length == 0)
            break;

        data = new byte[length];
        in.readFully(data);
    }

    if(data != null) {
        String response = new String(data);

        if(listener != null) {
            listener.onMessageReceived(response);
        }
    }
}
3
  • have you even tried the code? why wont it work? your question isnt very clear Commented Jun 9, 2014 at 16:57
  • cause I don't know what expression to do with the while loop Commented Jun 9, 2014 at 16:58
  • i still dont understand Commented Jun 9, 2014 at 16:59

1 Answer 1

2

Assuming you're trying to handle a stream of messages, sounds like what you're missing is a way of specifying (in the stream) how big your messages are (or where they end).

I suggest you just write a prefix before each message, specifying the length:

output.writeInt(data.length);
output.write(data);

Then when reading:

while (true)
{
    int length = input.readInt();
    byte[] buffer = new byte[length];
    input.readFully(buffer, 0, length);
    // Process buffer
}

You'll also need to work out a way of detecting the end of input. DataInputStream doesn't have a clean way of detecting that as far as I can tell. There are various options - the simplest may well be to write out a message of length 0, and break out of the loop if you read a length of 0.

Sign up to request clarification or add additional context in comments.

11 Comments

I'm guessing when you do bufferedReader.readLine() it keeps waiting for a break line. So what you are saying is that I should loop until a character sequence is met?
@ChristopherFrancisco: No, I didn't say that at all - if you're writing a byte array, there's no such thing as a character sequence. You could use a byte sequence instead, but usually it's better to just use a length prefix instead.
I see. Where should I put the if statement to break the loop when the length is 0? after readFully()?
Ok, now then, in the sending part, I'd put output.writeInt(0) after output.write(byte[]) right?
@ChristopherFrancisco: Well you'd put it outside whatever loop you've got - basically when you've sent all the messages. (If you're only sending a single message, you don't need any of this...)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.