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));
}
Rana
  • 3
  • 2
  • 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. – Germann Arlington Aug 06 '14 at 09:43
  • Tried to read as shown in original post. – Rana Aug 06 '14 at 09: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()`. – user207421 Aug 06 '14 at 09:50
  • You can use Integer.parseInt(hexString, 16) , as you can see in http://stackoverflow.com/questions/3834468/java-reading-hex-values-into-an-array-of-type-int – Mihai8 Aug 06 '14 at 09: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. – Germann Arlington Aug 06 '14 at 09:57
  • "It does not work" can be anything. Is there any output at standard output? What does it look like? – laune Aug 06 '14 at 10:08
  • Sender is sending string containing hex representation "\x00\x2C\x02\x00\xE0\x00". From java server side, trying to read as DataInputStream.getBytes but it is printing some strange characters with square shapes. – Rana Aug 06 '14 at 10:26

2 Answers2

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

A4L
  • 17,353
  • 6
  • 49
  • 70
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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138