浮点缓冲区转换为 unsigned char* 在套接字发送时丢失数据
有没有更好的方法通过套接字发送浮点数据? 下面是发送数据的实现。
static float theOUTPUT[THE_FLOAT_DATA_BUFFER_SIZE];
int size = 2048;
int tb = 0;
int numbytes = 0;
int cs = 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;
}
}
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 cs = 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在此代码中,不能保证
size
与sizeof(theOUTPUT)
匹配。如果较少,那么您将丢失数据。In this code, there's no guarantee that
size
matchessizeof(theOUTPUT)
. If it less, then you will lose data.感谢您的回答。这不是一个很好的问题。我发现了这个问题,并发现我通过套接字发送的数据比我想象的要多。这就是破坏数据的原因。
@用户470379
C 编程语言标准与浮点数无关,但我认为这就是你的意思。
如果您构建服务器和客户端,那么无论使用何种编程语言,您都可以控制数据编码方式。
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.