Linux C network programming: Use domain name to obtain IP address or IP to get the domain name network address conversion to integer host character sequence and network byte order conversion

Source: Internet
Author: User


Get an IP address with a domain name or get a domain name with IP
 
#include<stdio.h>
#include<sys/socket.h>
#include<netdb.h>
int main(int argc,char **aggv)
{
        struct hostent *host;
        char hostname[]="www.163.com";
        char hostname2[]="www.baidu.com";
        struct in_addr in;
        struct sockaddr_in addr_in;
        int h_errno;
        char addr[]="202.108.249.216";

        if((host=gethostbyname(hostname))!=NULL)
                {
                        memcpy(&addr_in.sin_addr.s_addr,host->h_addr,4);
                        in.s_addr=addr_in.sin_addr.s_addr;
                        printf("Domain name : %s \n",hostname);
                        printf("IP length is: %d \n",host->h_length);
                        printf("Type : %d \n",host->h_addrtype);
                        printf("IP : %s \n",inet_ntoa(in));

                }

        if(((host=gethostbyaddr(addr,sizeof(addr),AF_INET)))!=(struct hostnet *)NULL)
        {
                memcpy(&addr_in.sin_addr.s_addr,host->h_addr,4);
                        in.s_addr=addr_in.sin_addr.s_addr;
                        printf("\n---------------------\n");
                        printf("Domain name : %s \n",hostname);
                        printf("IP length is: %d \n",host->h_length);
                        printf("Type : %d \n",host->h_addrtype);
                        printf("IP : %s \n",inet_ntoa(in));
        }
        return 0;
}




#include <stdio.h>
#include <netdb.h>
#include <arpa / inet.h>
int main ()
{
         int i;
         for (i = 0; i <6; i ++)
         printf ("% d% s \ n", i, hstrerror (i)); // Catch the error number

         char cp [] = "192.168.0.84";
         printf ("% ld \ n", inet_addr (cp));
// Convert network address to integer
  struct in_addr ip; ip.s_addr = 16885952; printf ("% s \ n", inet_ntoa (ip));
// Convert integer to network address
  long local; int port; local = 123456; port = 1024; printf ("net:% d \ n", htonl (local)); // Conversion of host character order and network byte order printf ("net:% d \ n ", htonl (port)); printf (" local:% d \ n ", ntohl (htonl (local)));
  printf ("local:% d \ n", ntohl (htonl (port))); return 0;}


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.