I'd like to send some double precision floating point numbers over the network. (standard C, standard sockets) There is no htond or ntohd to convert the data to and from network byte order. What should I do? I have a couple solutions in my head but I'd like to know what the common practice is.
(I'd also like to know what is the common practice for sending 64bit ints, like gint64 values used by gstreamer)
edit: This is one solution I thought of. I presume it works for any size integers, but is it correct for doubles?
void swap_if_necessary (void* buff, int buff_len)
{
uint32_t foo = 1;
if ( htonl(foo) != foo )
{
char* to_swap = (char*)buff;
int i;
for (i = 0; i < buff_len/2; i++)
{
char swap_buff = to_swap[i];
to_swap[i] = to_swap[buff_len -1 -i];
to_swap[buff_len -1 -i] = swap_buff;
}
}
}
Again, it is rare to find two hosts directly connected to each other. But understanding what it takes to get a packet from one Host to another Host is key to understanding how a Switch enables multi-host communication, or a Router enables multi-network communication.
Since there are no Routers in this illustration, we know all the communication is happening within the same network — therefore, Host A and Host B are both configured with IP addresses that belong to the same network. Each host has a unique IP address and MAC address. Since each host is also a L3 device, they each also have an ARP Table.
In both cases, Host A is communicating with two hosts on foreign networks. Therefore, Host A will need to get either packet to its default gateway — R1. Host A will create the L3 header with a Source IP address of 11.11.11.77, and a Destination IP address of 22.22.22.88 (for Host B) or 33.33.33.99 (for Host C).
As such, this article will focus on host to host communication, and each individual step involved in the process. Since there are no Routers in this illustration, we know all the communication is happening within the same network — therefore, Host A and Host B are both configured with IP addresses that belong to the same network.
Convert it to an agreed string format and send that. Not sure if it's common practice or even decent, but it worked for me (it is true I did not care about performance at that point since I wasn't sending very many values).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With