0

My Socket client is written in C and sends the values in the format below:

x00\x2C\x02\x00\xE0\x00

Now I would like to read the hex values from the TCP/IP socket server which is written in Java.

What should be a approach to read the stream values in Java Socket?

I'm trying to read in the following way, but it does not work:

InputStream ins = sock.getInputStream();
int byteRead;

while((byteRead = ins.read()) != -1) {
  System.out.println(Integer.toHexString(byteRead & 0xFF));
}
7
  • Look at Socket in (and out) streams. The data as it is transmitted over the networks is always binary, so it only depends how you interpret it on receiving end. Commented Aug 6, 2014 at 9:43
  • Tried to read as shown in original post. Commented Aug 6, 2014 at 9:44
  • The 'format below' is literally that, a 'format'. The data is still binary. It is six bytes, value 0x002C0200E000. You can read that with DataInputStream.readShort()/.readInt(). Commented Aug 6, 2014 at 9:50
  • You can use Integer.parseInt(hexString, 16) , as you can see in stackoverflow.com/questions/3834468/… Commented Aug 6, 2014 at 9:51
  • 1
    Do you mean that the sender is literally sending a String containing hex representations? In this case you should be converting a String to integer, not other way around. Commented Aug 6, 2014 at 9:57

2 Answers 2

0

First stating the obvious, "\x41" is one byte with hex 41 aka ASCII 'A'. C strings also have a terminating \x00 following it. So you are writing

\x00\x2C\x02\x00\xE0\x00

as char array with size 6. Writing it as string would write an empty string, as the first byte is \x00.

On the java side there is no problem with \x00, that is just a regular byte/char.

The java code is okay. A variation might be:

try (InputStream ins = new BufferedInputStream(sock.getInputStream())) {
    int byteRead;
    while ((byteRead = ins.read()) != -1) {
        System.out.printf("%02X%n", byteRead & 0xFF);
    }
} // Closes

Here capital X causes capitalized hex, and %n a line break. String.format would be the alternative.

My first guess would be that the C writing is erroneously done with puts, using strlen or so.

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

1 Comment

Thanks for the solution. Print formatting was the clue to resolve our problem.
0

If you dont know the length of the message you will need to read each byte reperesentation into a String buffer and use Byte#parseByte(buffer, 16) to make a signed byte [-127, 127] out of it.

If you know the length of the whole String that the client is sending you could read it all and then String#split(regex) it using a delimiter whicht seems to be in your case \x. and again make use of Byte#parseByte(buffer, 16) to convert.

If you need unsigned values [0, 255] then you have to go with Integer.html#parseInt(buffer, 16).

Comments

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.