htons() function in socket programing

前端 未结 4 608
执笔经年
执笔经年 2020-12-22 19:06

I am new to socket programming and I am trying to understand the operation of htons(). I\'ve read a few tutorials on the Internet like this and this one for in

相关标签:
4条回答
  • 2020-12-22 19:19

    It is done to maintain the arrangement of bytes which is sent in the network(Endianness). Depending upon architecture of your device,data can be arranged in the memory either in the big endian format or little endian format. In networking, we call the representation of byte order as network byte order and in our host, it is called host byte order. All network byte order is in big endian format.If your host's memory computer architecture is in little endian format,htons() function become necessity but in case of big endian format memory architecture,it is not necessary.You can find endianness of your computer programmatically too in the following way:->

       int x = 1;
       if (*(char *)&x){
          cout<<"Little Endian"<<endl;
       }else{
          cout<<"Big Endian"<<endl;
       }
    

    and then decide whether to use htons() or not.But in order to avoid the above line,we always write htons() although it does no changes for Big Endian based memory architecture.

    0 讨论(0)
  • 2020-12-22 19:32

    It has to do with the order in which bytes are stored in memory. The decimal number 5001 is 0x1389 in hexadecimal, so the bytes involved are 0x13 and 0x89. Many devices store numbers in little-endian format, meaning that the least significant byte comes first. So in this particular example it means that in memory the number 5001 will be stored as

    0x89 0x13
    

    The htons() function makes sure that numbers are stored in memory in network byte order, which is with the most significant byte first. It will therefore swap the bytes making up the number so that in memory the bytes will be stored in the order

    0x13 0x89
    

    On a little-endian machine, the number with the swapped bytes is 0x8913 in hexadecimal, which is 35091 in decimal notation. Note that if you were working on a big-endian machine, the htons() function would not need to do any swapping since the number would already be stored in the right way in memory.

    The underlying reason for all this swapping has to do with the network protocols in use, which require the transmitted packets to use network byte order.

    0 讨论(0)
  • 2020-12-22 19:35

    htons is host-to-network short

    This means it works on 16-bit short integers. i.e. 2 bytes.

    This function swaps the endianness of a short.

    Your number starts out at:

    0001 0011 1000 1001 = 5001

    When the endianness is changed, it swaps the two bytes:

    1000 1001 0001 0011 = 35091

    0 讨论(0)
  • 2020-12-22 19:38

    the htons() function converts values between host and network byte orders. There is a difference between big-endian and little-endian and network byte order depending on your machine and network protocol in use.

    0 讨论(0)
提交回复
热议问题