1

I have an application that show CCTV feed from mobile. I successfully develop iOS application and now my client wants me to port to Android. The porting was fine until I'm stuck at this part of code.

This part of code, I have to connect to TCP socket server. When connected, I don't have to send server any thing, instead, server will send me a JPEG image. So, after connected, I'll have to keep reading until I received JPEG end marker (0xFF, 0xD9) and then close the connection.

What I plan to do is

Socket s = new Socket("Server IP Addreess", SERVER_PORT);
DataInputStream dis = new DataInputStream(socket.getInputStream());

ArrayList<Byte> bytes = new ArrayList<Byte>();
boolean err = false;

while (true) {
    byte b = dis.readByte();

    if (b == -1) {
        err = true;
        break;
    }        

    bytes.add(b);

    if ((bytes.get(bytes.size() - 1) == (byte) 0xFF) &&
        (bytes.get(bytes.size() - 2) == (byte) 0xD9)) {
        break;
    }
}

socket.close();

if (!err) {
    // create bitmap from byte array and show in ImageView
}

But I'm not sure that this is correct or not. The other solution I'm thinking about is

Socket s = new Socket("Server IP Addreess", SERVER_PORT);
BitmapFactory.decodeStream(s.getInputSteam());

But, also, I don't know how to close socket when server send 0xFF, 0xD9. Or will the BitmapFactory will detect that marker and close socket connection for me?

Any suggestion are welcome. Thank you!

P.S. I don't have test environment as they took it back when I delivered iOS app. So, I have to develop and then deliver to them to test (by playing with the app). Then if it's not working, they will tell me to correct it but I won't be able to access LogCat or other useful information. This is just like trying to sew a button in dark room, and I can't turn on the light.

: |

EDIT

More information about this server

  • Server won't send length of file, so, number of bytes for each file is unknown.
  • I have to detect 0xFF, 0xD9 that indicate end of file.
  • Problem is, I have to terminate socket connection from my side, server won't do it.
  • I can't change the way server works, it's hardware that my client purchase.

Also, I'm getting one image for each TCP connection, I'm not getting multiple images for single TCP connection.

2
  • They don't use any special protocol? Server closes connection after JPEG is sent?
    – Dario
    Jan 21, 2015 at 8:05
  • That's the thing, server won't terminate connection from their side, I have to do myself. It's kinda bad hardware design.
    – Tar_Tw45
    Jan 21, 2015 at 8:17

1 Answer 1

2

This is a bad idea to just look for some magic bytes to determine the end of the data. While these magic bytes should be at the end of the file they can also happen inside the data.

Better would be to either prefix the data with the length or use a separate TCP connection for each jpeg file and just read until the end of connection (that is until you don't get any more data). If this is not possible you have to do more advanced parsing, see Detect Eof for JPG images.

2
  • Thank for your commend. The thing is, what i know for sure are 1.) server will always send 0xFF, 0xD9 to indicate end of file 2.) server will send one image at a time (so, nothing come after 0xFF, 0xD9) 3.) BUT, I have to close socket connection from my side, server will not do it. (So, yes, I'm getting one image for each TCP connection, not multiple images)
    – Tar_Tw45
    Jan 21, 2015 at 8:09
  • Yes, the server will indicate end of file with FFD9 but you can not be sure that FFD9 is not also within the jpeg file itself. So you need to have a more advanced detection algorithm as described in the post I've linked too. Jan 21, 2015 at 8:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.