Objective
In socket programming, we often use the digital IP address and port number process programming, but we are usually familiar with some easy to remember character names, to make this name can be used for socket operation function recognition, so there must be some kind of conversion relationship between the two. This section describes the conversion between host name and address and service name and port number . In Unix systems, you can use the function gethostbyname, gethostbyaddr to implement the conversion between host name and address , you can use function Getservbyname, Getservbyport Implement the conversion between service name and port number . However, these functions are only suitable for IPv4 domains, and you can use the Getaddrinfo function if you want to implement these functions in IPv4 and IPV6.
Host nameAnd
AddressThe conversion between
GetHostByName and GETHOSTBYADDR functions
/* Convert between host name and address *//* * Function function: Convert between hostname and address; * Return value: Returns the host structure pointer if successful, or null if an error occurs; * Function prototype: */#include <netdb.h>struct hostent *get Hostbyname (const char *hostname);//convert hostname to numeric address, struct hostent *gethostaddr (const char *ADDR, size_t len, int family);// Convert numeric address to hostname;/* Function: Get host information; * Function prototype: */struct hostent *gethostent (void);/* Get host information and return hostent structure pointer */void sethostent (int Stayopen);/* Set host information */void endhostent (void);/* Description: * If the host data file is not open, gethostent will open it, the function returns the next entry of the file; * Function sethostent will open the file, If the file is open, wrap it around; * Function Endhostent will close the file; * hostent structure contains at least the following member data: */struct hostent{ char *h_name; /* Official name of host */ char **h_aliases; /* Pointer to alternate host name array */ int h_addrtype; /* Address type:af_inet */ int h_length; /* length in bytes of Address:4 */ char **h_addr_list; /* Pointer to array of IPV4 address */};
If the above function is called successfully, it returns a pointer to the hostent structure, returns NULL if an error occurs, and sets the global variable h_error to the corresponding value. The generic socket system call stores the error information in the global variable error, but the system call related to host hosts stores the error information in H_error, which is evaluated as follows:
- Host_not_found: Cannot find the host;
- try_again: retry;
- no_recovery: Non-repairable error;
- no_data: The specified name is valid, but not recorded;
Among them, the relationship between the hostent structure information is as follows:
Service NameAnd
Port numberThe conversion between
Getservbyname and Getservbyport functions
/* Conversion between service name and port number *//* * Function function: Conversion between service name and port number; * Return value: Returns a pointer if successful, or null if an error occurs; * Function prototype: */#include <netdb.h>struct servent *get Servbyname (const char *servname, const char *protoname); struct servent *getservbyport (int port, const char *protoname); St Ruct servent *getservent (void), void setservent (int stayopen), void endservent (void),/* * Protoname parameter if NULL, the return depends on the implementation, if not NULL, Specify the protocol name; * * Where the servent structure contains at least the following members: */struct servent{ char *s_name; /* Official service Name */ char **s_aliases; /* Pointer to alternate service name array */ int s_port; /* Port number */ char *s_proto; /* Name of protocol */};
The following is a client program written using the above function:
#include "unp.h" intmain (int argc, char **argv) {INTSOCKFD, n;charrecvline[maxline + 1];struct sockaddr_inservaddr; struct in_addr**pptr = null;struct in_addr*inetaddrp[2];struct in_addrinetaddr;struct hostent*hp;struct servent*sp;if (argc! = 3) err_quit ("Usage:%s
Conversion between "address and hostname" and "service name and port number"getaddrinfo function
/* IPV6, IPV4 can use the *//* * Function function: Convert service name and port number and hostname to address; * Return value: 0 If successful, return non 0 error code if error; * Function prototype: */#include <netdb.h> #incl Ude <sys/socket.h>int getaddrinfo (const char *hostname, const char *service, const struct addrinfo *hints, struct AD Drinfo **result), void Freeaddrinfo (struct addrinfo *ai);/* Returns the member structure memory dynamically allocated from the Getaddrinfo function to the system, The parameter AI is the first addrinfo structure returned by the function getaddrinfo */const char *gai_strerror (int error), or the error message can only be output by the function if getaddrinfo is faulted; */* Description: * The function needs to provide a host name or service name, and if only one is provided, the other must be specified as null; * Addrinfo is a structure linked list, which is defined as follows: */struct addrinfo{int ai_flags; /* Customize behavior */int ai_family; /* Address family */int ai_socktype; /* Socket type */int ai_protocol; /* protocol */socklen_t Ai_addrlen; /* length in bytes of address */struct sockaddr *ai_addr; /* Address */char *ai_canonname; /* Canonical Name of host */struct addrinfo *ai_next; /* Next in List */};/* * Function function: Convert address to service name or hostname; * Return value: 0 if successful, if errorReturns a non-0 value; * Function prototype: */#include <netdb.h> #include <sys/socket.h>int getnameinfo (const struct Sockadd *addr, socklen_t Alen, char * host, socklen_t Hostlen, char * service, socklen_t servlen, unsigned int flags); */* Description: * A Ddrinfo Structure Member: * Ai_flags value is as follows: * (1) ai_passive socket will be used for passive open; * (2) Ai_canonname tells the Getaddrinfo function to return the canonical name of the host; * (3) Ai_nume Richost prevent any type of name-to-address mapping, hostname must be an address string; * (4) Ai_numericserv prevent any type of name to the service map, the service must be a decimal port number string; * (5) ai_v4mapped if Also specify the ai_family value of Af_inet6, if there is no AAAA record available, the IPV6 address of the IPV4 map corresponding to the a record is returned; * (6) Ai_all If you specify the AI_V4MAPPED flag at the same time, in addition to returning the IPV6 address corresponding to the AAAA record , the IPV6 address of the IPV4 map corresponding to the a record is returned; * (7) Ai_addrconfig Select the return address type according to the configuration of the host; */
Resources:"Unix Network Programming"
"Network Programming" socket address and name conversion