float buffer casting to unsigned char* losing data on socket send
Is there a better way to send float data over a socket? Below is an implementation that sends the data.
static float theOUTPUT[THE_FLOAT_DATA_BUFFER_SIZE];
int size = 2048;
int tb = 0;
int numbytes = 0;
int c开发者_JAVA百科s = 256;
unsigned char* buf = (unsigned char*)theOUTPUT;
while(tb < size) {
numbytes = send(sock, buf, cs, 0);
printf("bytes sent: %i\n", numbytes);
tb+=numbytes;
buf+=numbytes;
if(tb >= size) {
break;
}
}
In this code, there's no guarantee that size
matches sizeof(theOUTPUT)
. If it less, then you will lose data.
Thanks for the answers. It was not a very good question. I discovered the issue and found that I was sending more data than I thought over the socket. This what was corrupting the data.
@user470379 C programming language standard is unrelated to floats but I think that is what you mean. If you build the server and the client then you have control over how you encode your data regardless of the programming language.
精彩评论