The multi-point host in the Network may have multiple IP addresses at the same time, especially when Dynamic Host address allocation is used, it is difficult to know what the IP address on the host is. The following uses a C program to list all IP addresses on the host. The code below is as follows:
Void print_all_ip (void)
{
Char szHostName [128];
Const char * pszAddr;
Struct hostent * pHost;
Int I, j;
If (gethostname (szHostName, 128) = 0)
{
PHost = gethostbyname (szHostName );
For (I = 0; pHost! = NULL & pHost-> h_addr_list [I]! = NULL; I ++)
{/* Process each IP Address */
PszAddr = inet_ntoa (* (struct in_addr *) pHost-> h_addr_list [I]);
Printf ("% s", pszAddr);/* print */
}
}
}
IntroductionThere are already many articles on Socket programming, so I will only briefly explain the relevant content. The gethostname function will return the information corresponding to the given host name. The definition of struct hostent in WinSock is as follows:
Struct hostent
{
Char FAR *H_name;
Char FAR * h_aliases;
ShortH_addrtype;
ShortH_length;
Char FAR * h_addr_list;
};