0

I want to send a byte[] array from a java client to a server that receives the data in C++. The byte array contains characters and integers that are converted to bytes (its a wave header). The server doesn't receive the values correctly. How can I send the byte[] so that the server socket can write it to a char[]? I am using the following code:

Client.java:

//Some example values in byte[]
byte[] bA = new byte[44];
bA[0]='R';
...
bA[4]=(byte)(2048 & 0xff);
...
bA[16] = 16;
....

//Write byte[] on socket
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.write(bA,0,44);

Server.cpp

int k = 0,n = 0;
char buffer[100];
ofstream wav("out.wav", ios::out | ios::binary);
while(k<44){//receive 44 values
    memset(buffer ,0 , 100);
    n = recv(sock , buffer , 100 , 0);
    k += n;
    buffer[99] = '\0';
    wav.write(buffer,n);
}
3
  • 2
    Do not null terminate buffer (the buffer[99] = '\0'; line). Since it is binary data, leave the data alone and rely on the return value of recv to determine how many bytes to copy to wav. Edit: I bet that this is the problem -- you're messing up the data with that line. Commented Jun 3, 2014 at 18:59
  • Maybe print the array in each language (as hex for example). Is there any obvious pattern? Are some parts correct and some not? Is everything offset by the same amount (would indicate a signed/unsigned issue possible)? Commented Jun 3, 2014 at 19:01
  • ba[4]=(byte)(2048 & 0xff): 2048 is too big for a byte. Is this a bug? ba[4] will always be 0. Commented Jun 3, 2014 at 19:07

1 Answer 1

2

One issue I see is if you receive 100 characters, you're corrupting the data with this line:

buffer[99] = '\0';

If there is a character other than NULL at that position, you've corrupted the data. Since the data is binary, there is no need to null terminate the buffer. Remove that line from your loop.

Instead, rely on the return value of recv to determine the number of characters to copy to the stream. Which brings up another point -- you're not checking if recv returns an error.

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

5 Comments

@user2212461 I'm curious; was it setting [99] to 0 or not checking the return value that was the source of the problem?
the null termination was the reason. With the code above I always use first one character and then the rest. (Always two while loops). Do you know why?
@user2212461 I'm a bit surprised by that; I'd have thought since you were only receiving 44 bytes, it wouldn't matter what was at index 99 anyway.
I understand your comment and I don't understand why this seemed to make trouble but now it works.
@user2212461 Maybe the Java code has changed and is now sending out 100 bytes. My focus was on the C++ code, and it definitely will not work if for some reason, you're receiving up to 100 bytes of binary data.

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.