Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

host to network double?

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;
        }  
    }
}
like image 744
dec-vt100 Avatar asked May 21 '11 20:05

dec-vt100


People also ask

Is it possible to have two hosts connected to each other?

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.

What is the relationship between host a and host B?

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.

How does host a communicate with two hosts on a foreign network?

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).

How does host to host communication work?

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.


1 Answers

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).

like image 170
cnicutar Avatar answered Sep 22 '22 01:09

cnicutar