4

Through a socket I am sending information from a program written in C to a program written in Java.

Through the program in C, I am sending two bytes through a char array (using an internet socket), and the received information in Java is stored in a char array also.

My main problem is that the received information in the Java array does not correspond properly to the transmitted information from the C program.

I have read that the chars in Java are 16 bits long, and the chars in C are 8 bits long. That may be the problem, but I do not know how to handle/solve that.

The C code to send information is the following:

char buffer[256];
bzero(buffer,256);
n = read(fd, buffer, 255);  // getting info from an uart port, which works properly
n = write(sockfd,buffer,3); // send the information through the socket

Part of the Java code (for an Android app) is the following:

char[] buffer = new char[256];
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
int readX = in.read(buffer, 0, 3);
if (readX > 0) { // I am using a handler to manipulate the info
    Message msg = new Message();
    msg.obj = buffer;
    mHandler.sendMessage(msg);
} 
....
// Part of the handler is the following:
mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
            char[] buffer;
            buffer = (char[])msg.obj; // the information here is different from the one sent from the C program
            ....
    }
}

Any suggestion to solve this problem I would really appreciate it.

Thanks in advance, Gus.

gus
  • 355
  • 2
  • 5
  • 19
  • 1
    If you can isolate a "minimal case" that fails in the same way, presenting the C source and the Java source, it would (possibly) help. – Vatine Nov 06 '12 at 17:41
  • There could be lots of ways in which your code gets this wrong. I concur with Vatine that seeing the code would help us help you. – NPE Nov 06 '12 at 17:41

2 Answers2

5

In C and C++ the char data type is 8-bit characters, corresponding roughly to the Java byte type. In Java, the fundamental char type is a 16-bit Unicode character. When you convert from bytes to characters (or vice-versa) in Java, a mapping has to occur, depending on the character encoding of the byte stream (UTF-8, ISO-8859-1, etc), so you have to know how the C byte stream is encoded. In your case I'd guess it's ISO-8859-1. If it's really binary data, then you should use the Java byte type.

EDIT:

You have to know whether the data being sent from C is character or binary, and if character, how the C program is encoding the data (ISO-8859-1, UTF-8, etc).

If the data is binary then you must use BufferedInputStream to read bytes, not BufferedReader, which decodes bytes into characters, which you don't want since the data is binary.

If the data is character, then you can use the 2-argument constructor

InputStreamReader(InputStream in, String charSetName)

or one of the other 2-arg constructors that let you specify how to decode bytes into characters.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • Thanks for the answer, but the problem doing so is that the function read from the class BufferedReader does not accept a byte type as argument. About the conversion you mentioned, can you provide any code to do that in Java?, or another alternative? Thanks. – gus Nov 12 '12 at 04:42
  • Thanks a lot for the info, with the info you provided for byte transmission through socket I solved the problem I was facing. – gus Nov 13 '12 at 22:42
1

Use the Java byte type, which is an 8-bit signed integer. Also ensure that your char type in C is actually 8 bits. The Boost StaticAssert facility can be used to ensure that.

eh9
  • 7,340
  • 20
  • 43