inet_addr ()Description: Converts a point-interval address into a in_addr.
#include <winsock.h>
unsigned long PASCAL FAR inet_addr (const struct far* cp);
CP: An Internet standard "." The string of intervals.
Comments:
This function interprets the string in the CP argument, which uses the Internet's "." The interval format represents the Internet address of a number. The return value can be used as an Internet address. All Internet addresses are returned in network byte order (bytes are arranged from left to right).
Internet address with "." The address of the interval can be expressed in the following ways:
A.b.c.d,a.b.c,a.b,a
When four parts are fixed, each is interpreted as a byte of data, and the Internet four-byte address is composed from left to right. Note that when an Internet address is represented on an Intel machine as a 32-bit integer, the above byte is "D.C.B.A". This is because the bytes of the Intel processor are arranged from right to left.
Please note that only Berkeley supports the following expressions and is not supported by the rest of the Internet. In view of the compatibility with the software, should be used in accordance with the provisions.
For a three-part address, the last part is interpreted as 16-bit data and is the right two bytes of the network address. In this way, the three-part address can easily represent a group B network address, such as "128.net.host".
For a two-part address, the last part is interpreted as 24-bit data and as the right three bytes of the network address, which makes it easy for the two-part address to represent a group C network address, such as "Net.host".
For an address that has only one part, its value is stored directly into the network address without any byte reorganization.
return value:
If no error occurs, INET_ADDR () returns an unsigned long integer that holds the Internet address in the appropriate byte order. If the incoming string is not a legitimate Internet address, as if any of the "a.b.c.d" addresses are more than 255, then inet_addr () returns Inaddr_none.
See:
Inet_ntoa (). implementation of inet_addr () function
Enter a string of dotted IP address formats (such as A.B.C.D) that extracts each part of the string, converts it to ulong, and assumes 4 ulong type a,b,c,d,
Uladdress (ULONG type) is the result of the conversion,
uladdress = d<<24 + c<<16 + b<<8 + A (network byte order), that is, the return result of inet_addr (const char *)
In addition, we can also get the result of converting the IP into a host sequence, the same as the conversion method
a<<24 + b<<16 + c<<8 + D
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, NULL is returned. The data should be copied before the next Windows socket interface is invoked.
The test code is as follows
Include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
int main (int aargc, char* argv[])
{
struct IN_ADDR addr1,addr2;
ULONG 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.Inet_aton ()Inet_aton is an improved way to convert a string IP address to a 32-bit network sequence IP address. The summary of this function is as follows:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int Inet_aton (const char *string, struct in_addr *addr);
The Inet_aton function accepts two parameters. The parameters are described as follows:
1 The input parameter string contains the IP address represented by ASCII.
The 2 output parameter addr is the structure that will be updated with the new IP address.
If this function succeeds, the return value of the function is Non-zero. If the input address is incorrect, zero is returned. Using this function does not have the error code stored in the errno, so his value is ignored.
One bit of confusion about this function is the two parameters required for this function call. If we define a af_inet set of interface addresses:
struct sockaddr_in adr_inet; * Af_inet * *
The parameter pointer provided to the Inet_aton function call is &ADR_INET.SIN_ADDR
The following program uses the Inet_aton function instead of the IN_ADDR function we talked about earlier.
/*
* INETATON.C
*
* Example using Inet_aton
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/*
* This function reports the error and
* Exits back to the shell
*/
static void bail (const char *on_what)
{
Fputs (On_what,stderr);
Fputs ("n", stderr);
}
int main (int argc,char **argv)
{
int z;
struct sockaddr_in adr_inet; * Af_inet * *
int len_inet; /* Length * *
int sck_inet; * * Socket/*
/* Create a Socket * *
Sck_inet = socket (af_inet,sock_stream,0);
if (sck_inet = = 1)
Bail ("Socket ()");
/* Establish address * *
memset (&adr_inet,0,sizeof adr_inet);
adr_inet.sin_family = af_inet;
Adr_inet.sin_port = htons (9000);
if (!inet_aton ("127.0.0.1", &adr_inet.sin_addr))
Bail ("bad address");
len_inet = sizeof adr_inet;
/* Bind it to the socket * * *
Z = Bind (sck_inet, (struct sockaddr *) &adr_inet,len_inet);
if (z = = 1)
Bail ("bind ()");
* Display Our sockets address * *
System ("Netstat-pa--tcp 2>/dev/null"
" | grep Inetaton ");
return 0;
}
The results of the program's operation are as follows:
s$./inetaton
TCP 0 0 127.0.0.23:9000 *:* Close 1007/inetaton