2

I'm trying to send data over network from a Java program to a C program. But I don't know which type of data to use. I tried with a char.

Here is my Java code :

DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
char c = 'c';
dos.writeChar(c);

And my C code :

char buffer[256];
bzero(buffer, 256);
read(newsockfd, buffer, 255);
printf("Here is the message: %c\n", buffer[0]);

"Here is the message" print nothing. I don't know if buffer[0] is empty or if the type is not compatible.

14
  • Yes I can use Json, but I just have an Int so send. I think it would be too heavy and complicated just for that, isn't ? Commented Mar 29, 2017 at 8:53
  • 1
    Do you check for error? What does read return? Commented Mar 29, 2017 at 8:55
  • 1
    How about: OutputStream os = sock.getOutputStream(); os.write((byte) c); Commented Mar 29, 2017 at 8:56
  • There is no error with read, I checked it Commented Mar 29, 2017 at 8:57
  • 2
    I think DataOutputSteam is a Wrapper for easy communication for Java. To communicate with another program of different language, it would better to use the most primitive stream. Commented Mar 29, 2017 at 9:00

1 Answer 1

2

in java a char is two bytes, you may use the java type "byte" to just send one byte. Or you have to support the two-byte character (unicode) sent by Java, to be printed in C.

Yet I did not have the time to check the rest of your code, but this might be a start.

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

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.