从服务器发送浮点数到客户端 [英] Sending floating point number from server to client
问题描述
我正在使用 TCP / IP 套接字编程。我在我的服务器代码中存储了一个存储在变量 ret_val 中的 浮点值 ,我想要发送给正在等待接收的客户端。
I am using TCP/IP socket programming. I have a floating point value stored in a variable ret_val in my server code which I want to send to the client which is waiting to receive it.
如何才能做到这一点?
How can I do it?
推荐答案
客户端和服务器是相同的平台等等,你可以简单地使用 sizeof(float)
来确定你的缓冲区大小,并从你的float地址复制那么多字节。
If you know that both client and server are the same platform etc., you can simply use sizeof(float)
to determine your buffer size and copy that many bytes from the address of your float.
float number = 123.45;
send(sockfd, &number, sizeof(float),0);
只要您的客户端/服务器是不同的平台/不同的语言等,您必须启动担心如何可移植地编码浮动。但是,对于一个简单的方法,上述将正常工作。
As soon as your client/server are different platforms/different languages etc. you'll have to start worrying about how to portably encode the float. But for a simple approach, the above will work fine.
这篇关于从服务器发送浮点数到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!