Ntohs, Ntohl, htons,htonl comparison and detailed explanation

Source: Internet
Author: User
Tags string format port number htons

Ntohs =net to host short int 16 bits
Htons=host to net short int 16 bits
Ntohl =net to host long int 32 bits
Htonl=host to net long int 32 bits

Briefly:
Converts an unsigned short integer number from network byte order to host byte order.
#include <winsock.h>
U_short PASCAL FAR ntohs (u_short netshort);
Netshort: A 16-digit number expressed in network byte order.
Comments:
This function converts a 16-digit number from network byte order to host byte order.
return value:
Ntohs () Returns a number expressed in host byte order.
  

Converts the unsigned short integer number of hosts to network byte order.
#include <winsock.h>
U_short PASCAL FAR htons (u_short hostshort);
Hostshort: 16 Digits of host byte order expression.
Comments:
This function converts a 16-digit number from host byte order to network byte order.
return value:
Htons () returns the value of a network byte order.
  

These 2 functions provide a conversion of host byte order and network byte order
Like a network byte of 00 01.
U_short A; How to directly correspond the words a=0100; Why, then? Since the host is from high byte to low byte, it should be converted to A=NTOHS (0001); Such a=0001;

The

Htonl () represents the conversion of a 32-bit host byte order to a 32-bit network byte order htons () represents the conversion of 16-bit host byte order the 16-bit network byte order (where the IP address is a 32-bit port number is 16-bit)

Converts an IP address to an integral type: first , suppose you already have a SOCKADDR_IN structure ina, you have an IP address "132.241.5.10" to store in it, you need to use the function inet_addr (), the IP address from the number format to the unsigned long integer type. The use method is as follows: Ina.sin_addr.s_addr = inet_addr ("132.241.5.10");
Note that the address returned by INET_ADDR () is already a network byte format, so you do not need to call the function htonl () again.
We now find that the code fragment above is not very complete because it has no error checking. Obviously, when inet_addr () has an error, return-1. Remember these binary digits. (unsigned number)-1 only matches the IP address 255.255.255.255. But it's a broadcast address. So, remember to do error checking first.

How to output a IN_ADDR structure to a number of points format. You want to use the function Inet_ntoa () ("Ntoa" means "network to ASCII"), just like this: printf ("%s", Inet_ntoa (INA.SIN_ADDR)), and it will output an IP address. It should be noted that Inet_ntoa () IN_ADDR the structure as an argument, not a long shaping. The same thing to note is that it returns a pointer to a character. It is a static, fixed pointer controlled by Inet_ntoa (), so each time the Inet_ntoa () is invoked, it overrides the IP address that was obtained at the time of the last call. For example:
Char *a1, *A2
.
.
A1 = Inet_ntoa (INA1.SIN_ADDR);
A2 = Inet_ntoa (ina2.sin_addr);
printf ("Addre SS 1:%s ", A1);
printf ("Address 2:%s", a2); The
output is as follows:
address 1:132.241.5.10
Adress 2:132.241.5.10
If you need to save this IP, use the strcopy () function to point to your own character pointer.

Inet_ntoa ()
Briefly:
Converts the network address to "." The dotted string format.
#include <winsock.h>
Char far* PASCAL FAR inet_ntoa (struct in_addr in);
In: A structure that represents the address of an Internet host.
Comments:
This function converts an Internet address structure represented by the in parameter to a string form such as "a.b.c.d" with the "." Interval. Note that the string returned by Inet_ntoa () is stored in the memory allocated by the Windows sleeve interface implementation. The application should not assume that the memory is allocated. Data is guaranteed to be valid until the next Windows socket interface is invoked on the same thread.
return value:
If no error occurs, Inet_ntoa () returns a character pointer. Otherwise, return to NVLL. The data should be copied before the next Windows socket interface is invoked.
See also: inet_addr ().


The test code is as follows
#pragma comment (lib, "Ws2_32.lib")

Noths.obj:error lnk2001:unresolved External Ymbol _inet_addr@4
#include <winsock.h>
#include <iostream.h>
#include <stdio.h>
int main (int aargc, char* argv[])
{
struct IN_ADDR addr1,addr2;
unsigned long l1,l2;
l1= inet_addr ("192.168.0.74");
L2 = inet_addr ("211.100.21.179");
memcpy (&ADDR1, &L1, 4);
memcpy (&ADDR2, &L2, 4);

printf ("%s:%s \ n", Inet_ntoa (ADDR1), Inet_ntoa (ADDR2)); Notice the result of the operation of this sentence

printf ("%s \ n", Inet_ntoa (ADDR1));
printf ("%s \ n", Inet_ntoa (ADDR2));
return 0;
}
The actual operating results are as follows:
192.168.0.74:192.168.0.74//From here you can see that the Inet_ntoa in printf runs only once.
192.168.0.74
211.100.21.179
Inet_ntoa returns a char *, and the space of this char * is statically allocated within the INET_NTOA, so the call after Inet_ntoa overwrites the last call. The result of the first printf can only show that the variable parameters in printf are evaluated from right to left, and that's it.


Http://blog.sina.com.cn/s/blog_59af98130100g32n.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.