Linux Network Programming BASICS (2)

Source: Internet
Author: User

1. byte conversion functions

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.

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. 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.

Int inet_aton (const char * CP, struct in_addr * indium)
Char * inet_ntoa (struct in_addr in)

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.

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.

5. Sample program

/*******... (Hostname_ip.c )************/
# Include "stdio. H"
# Include "stdlib. H"
# Include "errno. H"
# Include "sys/types. H"
# Include "sys/socket. H"
# Include "unistd. H"
# Include "netinet/in. H"
# Include "netdb. H"

Int main (INT argc, char ** argv)
{
Struct sockaddr_in ADDR;
Struct hostent * Host;
Char ** alias;

If (argc <2 ){
Fprintf (stderr, "Usage: % s hostname | IP .. A", argv [0]);
Exit (1 );
}

Argv ++;
For (; * argv! = NULL; argv ++ ){
/*... IP */
If (inet_aton (* argv, & ADDR. sin_addr )! = 0 ){
Host = gethostbyaddr (char *) & ADDR. sin_addr, 4, af_inet );
Printf ("address information of IP % s", * argv );
}
Else {
/*..,.....? */
Host = gethostbyname (* argv );
Printf ("address information of host % s", * argv );
}
If (host = NULL ){
/*...,.....*/
Fprintf (stderr, "No address information of % s", * argv );
Continue;
}
Printf ("Official host name % s", host-> h_name );
Printf ("name aliases :");
For (alias = Host-> h_aliases; * alias! = NULL; alias ++)
Printf ("% s,", * alias );
Printf ("IP Address :");
For (alias = Host-> h_addr_list; * alias! = NULL; alias ++)
Printf ("% s,", inet_ntoa (* (struct in_addr *) (* alias )));
}
}

 

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.