Linux Network Programming-3. Information functions of servers and clients
Source: Internet
Author: User
Article Title: Linux Network Programming-3. server and client information functions. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
In this chapter, we will learn information functions in terms of conversion and network.
3.1-byte Conversion Function
There are many types of machines on the network. These machines indicate different bytes of data. For example, the i386 chip is low-byte at the low-end of the memory address, and the high-byte is high-end, the alpha chip is the opposite. in Linux, there is a dedicated byte conversion function.
Unsigned long int htonl (unsigned long int hostlong)
Unsigned short int htons (unisgned short int hostshort)
Unsigned long int ntohl (unsigned long int netlong)
Unsigned short int ntohs (unsigned short int netshort)
In these four conversion functions, h represents host, and n represents network. s stands for short l stands for long. The first function means to convert long data on the machine into long data on the network. the meanings of other functions are similar.
3.2 IP and domain name Conversion
The IP address or domain name can be used to mark a machine on the network. How can we convert it?
Struct hostent * gethostbyname (const char * hostname)
Struct hostent * gethostbyaddr (const char * addr, int len, int type)
There is a definition of struct hostent in it
Struct hostent {
Char * h_name;/* Host name */
Char * h_aliases;/* Host alias */
Int h_addrtype;/* Host address type AF_INET */
Int h_length;/* The host address length is 4 bytes and 32 characters for IP4 */
Char ** h_addr_list;/* List of Host IP addresses */
}
# Define h_addr h_addr_list [0]/* The first IP address of the Host */
Gethostbyname can be used to convert a machine name (such as linux.yessun.com) to a structure pointer. The domain name information is stored in this structure.
Gethostbyaddr can convert a 32-bit IP address (C0A80001) to a structure pointer.
When the two functions fail, return NULL and set the h_errno error variable. Call h_strerror () to obtain detailed error information.
3.3 string IP address and 32-bit IP address conversion.
The IP addresses we use on the network are composed of numbers (192.168.0.1), while the 32-bit IP address is used in the struct in_addr structure. The above 32-bit IP address (C0A80001) yes, 192.168.0.1. We can use the following two functions for conversion.
In the function, a indicates that ascii n indicates network. the first function indicates. b. c. d's IP address is converted to a 32-bit IP address, which is stored in the indium pointer. the second is to convert a 32-bit IP address to. b. c. d format.
3.4 service information functions
In network programs, we sometimes need to know port. IP and service information. In this case, we can use the following functions:
Int getsockname (int sockfd, struct sockaddr * localaddr, int * addrlen)
Int getpeername (int sockfd, struct sockaddr * peeraddr, int * addrlen)
Struct servent * getservbyname (const char * servname, const char * protoname)
Struct servent * getservbyport (int port, const char * protoname)
Struct servent
{
Char * s_name;/* Official service name */
Char ** s_aliases;/* alias list */
Int s_port;/* Port Number */
Char * s_proto;/* protocol used */
}
These functions are rarely used. corresponding to the client. When we want to obtain the port number of the connection, use the port number allocated by the system after the connect call is successful. after INADDR_ANY is used for the server, we can use the IP address after the accept call is successful to obtain the connected IP address.
There are many default ports and services on the network. For example, port 21 corresponds to ftp80 and WWW. to obtain the service with the specified port number, we can call the fourth function. To obtain the port number, we can call the third function.
3.5 example
# Include
# Include
# Include
# Include
# Include
Int main (int argc, char ** argv)
{
Struct sockaddr_in addr;
Struct hostent * host;
Char ** alias;
If (argc <2)
{
Fprintf (stderr, "Usage: % s hostname | ip .. \ n \ a", argv [0]);
Exit (1 );
}
Argv ++;
For (; * argv! = NULL; argv ++)
{
/* Assume the IP address */
If (inet_aton (* argv, & addr. sin_addr )! = 0)
{
Host = gethostbyaddr (char *) & addr. sin_addr, 4, AF_INET );
Printf ("Address information of Ip % s \ n", * argv );
}
Else
{
/* Failed. Is it a domain name? */
Host = gethostbyname (* argv); printf ("Address information
Of host % s \ n ", * argv );
}
If (host = NULL)
{
/* None of them. Forget it */
Fprintf (stderr, "No address information of % s \ n", * argv );
Continue;
}
Printf ("Official host name % s \ n", host-> h_name );
Printf ("Name aliases :");
For (alias = host-> h_aliases; * alias! = NULL; alias ++)
Printf ("% s,", * alias );
Printf ("\ nIp address :");
For (alias = host-> h_addr_list; * alias! = NULL; alias ++)
Printf ("% s,", inet_ntoa (* (struct in_addr *) (* alias )));
}
}
In this example, we call two functions to determine whether the user entered an IP address or a domain name. The first time we assume that the user entered an IP address, we call inet_aton, call gethostbyname to obtain the information.
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.