1. Introduction
We often encounter a computer name that a computer that needs to get an IP address in the network programming. Or know the IP address of the computer, but you need to know the case of its machine name. This can be implemented using a socket. In order to better facilitate everyone, the author is here a simple introduction.
2. related functions
l inet_addr
Prototype: unsigned long inet_addr (const char far * cp);
Parameters: CP, input parameters, IP address of the computer
Return: The address of the IN_ADDR structure
l inet_ntoa
Prototype: Char Far * inet_ntoa (struct in_addr in);
Parameters: IN, input parameters, in_addr structure
Returns: the name of the calculated
l GethostbyAddr
Prototype: Struct Hostent Far * GethostbyAddr (Const Char Far * Addr, Int Len, Int Type);
Parameters: addr, input parameters, computer addresses
Len, input parameters, address length
TYPE, input parameters, address type
Return: Host Information
l gethostbyname
Prototype: Struct Hostent Far * gethostByname (Const Char Far * Name);
Parameters: Name, computer name
Returns: Computer Information
3. IP address to computer name
// function name: gethostname
// Description: Get the machine name through the IP address
// Return Type: INT machine name string length
// argument: char * Addr IP address, for example 192.168.1.3
// argument: char * Name machine name
INT gethostname (char * addr, char * name)
{
Unsigned int node = inet_addr (addr);
IF (Node == INADDR_NONE)
Return -1;
Word Ver = MakeWord (2, 2);
Wsadata wsadata;
IF (WSAStartup (Ver, & Wsadata)! = 0) // Start Winsock
Return -1;
Struct hostent * phost = gethostbyaddr ((char *) & node, 4, af_inet); // Get machine information
IF (! Phost)
Return -1;
INT LEN = Strlen (phost-> h_name);
Memcpy (name, phost-> h_name, len);
Return Strlen (Name);
}
4. Computer name to IP address
// function name: gethostip
// Description: Get the IP address of the machine
// RETURN TYPE: INT IP address string length
// argument: const char * name machine name, such as: zhangzhiqiang
// argument: char * ip [out] machine IP address
INT gethostip (const char * name, char * ip) {
Wsadata wsadata;
IF (WsaStartup (MakeWord (2, 2), & WSADATA)! = 0)
{
Trace ("Start Socket Error! / N");
Return False;
}
Hostent * host = gethostbyname (name);
IF (! Host)
Return -1;
Struct SockAddr_in Mac;
Mac.sin_family = af_INet;
Memcpy (& mac.sin_addr, host-> h_addr_list [0], host-> h_length);
// Get the actual IP address
Char * ipaddr = inet_ntoa (mac.sin_addr);
IF (! ip)
Return -1;
Memcpy (IP, IPADDR, Strlen (iPaddr));
Return Strlen (iPaddr);
}
5. Small knot
The methods mentioned herein are Socket programming, and the code can be applied slightly to support sockets such as Linux. The code used in this paper is tested on the Windows 2000 machine.
6. Take
In fact, this is just a small skill, and it is simple for those who are familiar with it. However, it is indeed helpful for people who have trailed online programming or just start learning to learn network programming. I published it and hoped that the programmer like me can reduce some time to explore.