linux中查询dns示例_C 语言

dns.c

复制代码 代码如下:

/*
 * DNS Query Program on Linux
 *
 * Author : ismdeep@live.com
 *
 * */
//Header Files
#include<stdio.h> //printf
#include<string.h> //strlen
#include<stdlib.h> //malloc
#include<sys/socket.h> //you know what this is for
#include<arpa/inet.h> //inet_addr , inet_ntoa , ntohs etc
#include<netinet/in.h>
#include<unistd.h> //getpid

//List of DNS Servers registered on the system
char dns_servers[10][100];
int dns_server_count = 0;
//Types of DNS resource records :)

#define T_A 1 //Ipv4 address
#define T_NS 2 //Nameserver
#define T_CNAME 5 // canonical name
#define T_SOA 6 /* start of authority zone */
#define T_PTR 12 /* domain name pointer */
#define T_MX 15 //Mail server

//Function Prototypes
void ngethostbyname (unsigned char* , int);
void ChangetoDnsNameFormat (unsigned char*,unsigned char*);
unsigned char* ReadName (unsigned char*,unsigned char*,int*);
void get_dns_servers();

//DNS header structure
struct DNS_HEADER
{
 unsigned short id; // identification number

 unsigned char rd :1; // recursion desired
 unsigned char tc :1; // truncated message
 unsigned char aa :1; // authoritive answer
 unsigned char opcode :4; // purpose of message
 unsigned char qr :1; // query/response flag

 unsigned char rcode :4; // response code
 unsigned char cd :1; // checking disabled
 unsigned char ad :1; // authenticated data
 unsigned char z :1; // its z! reserved
 unsigned char ra :1; // recursion available

 unsigned short q_count; // number of question entries
 unsigned short ans_count; // number of answer entries
 unsigned short auth_count; // number of authority entries
 unsigned short add_count; // number of resource entries
};

//Constant sized fields of query structure
struct QUESTION
{
 unsigned short qtype;
 unsigned short qclass;
};

//Constant sized fields of the resource record structure
#pragma pack(push, 1)
struct R_DATA
{
 unsigned short type;
 unsigned short _class;
 unsigned int ttl;
 unsigned short data_len;
};
#pragma pack(pop)

//Pointers to resource record contents
struct RES_RECORD
{
 unsigned char *name;
 struct R_DATA *resource;
 unsigned char *rdata;
};

//Structure of a Query
typedef struct
{
 unsigned char *name;
 struct QUESTION *ques;
} QUERY;

int main( int argc , char *argv[])
{
 unsigned char hostname[100];

 //Get the DNS servers from the resolv.conf file
 get_dns_servers();

 //Get the hostname from the terminal
 printf("Enter Hostname to Lookup : ");
 scanf("%s" , hostname);

 //Now get the ip of this hostname , A record
 ngethostbyname(hostname , T_A);

 return 0;
}

/*
 * Perform a DNS query by sending a packet
 * */
void ngethostbyname(unsigned char *host , int query_type)
{
 unsigned char buf[65536],*qname,*reader;
 int i , j , stop , s;

 struct sockaddr_in a;

 struct RES_RECORD answers[20],auth[20],addit[20]; //the replies from the DNS server
 struct sockaddr_in dest;

 struct DNS_HEADER *dns = NULL;
 struct QUESTION *qinfo = NULL;

 printf("Resolving %s" , host);

 s = socket(AF_INET , SOCK_DGRAM , IPPROTO_UDP); //UDP packet for DNS queries

 dest.sin_family = AF_INET;
 dest.sin_port = htons(53);
 dest.sin_addr.s_addr = inet_addr(dns_servers[0]); //dns servers

 //Set the DNS structure to standard queries
 dns = (struct DNS_HEADER *)&buf;

 dns->id = (unsigned short) htons(getpid());
 dns->qr = 0; //This is a query
 dns->opcode = 0; //This is a standard query
 dns->aa = 0; //Not Authoritative
 dns->tc = 0; //This message is not truncated
 dns->rd = 1; //Recursion Desired
 dns->ra = 0; //Recursion not available! hey we dont have it (lol)
 dns->z = 0;
 dns->ad = 0;
 dns->cd = 0;
 dns->rcode = 0;
 dns->q_count = htons(1); //we have only 1 question
 dns->ans_count = 0;
 dns->auth_count = 0;
 dns->add_count = 0;

 //point to the query portion
 qname =(unsigned char*)&buf[sizeof(struct DNS_HEADER)];

 ChangetoDnsNameFormat(qname , host);
 qinfo =(struct QUESTION*)&buf[sizeof(struct DNS_HEADER) + (strlen((const char*)qname) + 1)]; //fill it

 qinfo->qtype = htons( query_type ); //type of the query , A , MX , CNAME , NS etc
 qinfo->qclass = htons(1); //its internet (lol)

 printf("\nSending Packet...");
 if( sendto(s,(char*)buf,sizeof(struct DNS_HEADER) + (strlen((const char*)qname)+1) + sizeof(struct QUESTION),0,(struct sockaddr*)&dest,sizeof(dest)) < 0)
 {
  perror("sendto failed");
 }
 printf("Done");

 //Receive the answer
 i = sizeof dest;
 printf("\nReceiving answer...");
 if(recvfrom (s,(char*)buf , 65536 , 0 , (struct sockaddr*)&dest , (socklen_t*)&i ) < 0)
 {
  perror("recvfrom failed");
 }
 printf("Done");

 dns = (struct DNS_HEADER*) buf;

 //move ahead of the dns header and the query field
 reader = &buf[sizeof(struct DNS_HEADER) + (strlen((const char*)qname)+1) + sizeof(struct QUESTION)];

 printf("\nThe response contains : ");
 printf("\n %d Questions.",ntohs(dns->q_count));
 printf("\n %d Answers.",ntohs(dns->ans_count));
 printf("\n %d Authoritative Servers.",ntohs(dns->auth_count));
 printf("\n %d Additional records.\n\n",ntohs(dns->add_count));

 //Start reading answers
 stop=0;

 for(i=0;i<ntohs(dns->ans_count);i++)
 {
  answers[i].name=ReadName(reader,buf,&stop);
  reader = reader + stop;

  answers[i].resource = (struct R_DATA*)(reader);
  reader = reader + sizeof(struct R_DATA);

  if(ntohs(answers[i].resource->type) == 1) //if its an ipv4 address
  {
   answers[i].rdata = (unsigned char*)malloc(ntohs(answers[i].resource->data_len));

   for(j=0 ; j<ntohs(answers[i].resource->data_len) ; j++)
   {
    answers[i].rdata[j]=reader[j];
   }

   answers[i].rdata[ntohs(answers[i].resource->data_len)] = '\0';

   reader = reader + ntohs(answers[i].resource->data_len);
  }
  else
  {
   answers[i].rdata = ReadName(reader,buf,&stop);
   reader = reader + stop;
  }
 }

 //read authorities
 for(i=0;i<ntohs(dns->auth_count);i++)
 {
  auth[i].name=ReadName(reader,buf,&stop);
  reader+=stop;

  auth[i].resource=(struct R_DATA*)(reader);
  reader+=sizeof(struct R_DATA);

  auth[i].rdata=ReadName(reader,buf,&stop);
  reader+=stop;
 }

 //read additional
 for(i=0;i<ntohs(dns->add_count);i++)
 {
  addit[i].name=ReadName(reader,buf,&stop);
  reader+=stop;

  addit[i].resource=(struct R_DATA*)(reader);
  reader+=sizeof(struct R_DATA);

  if(ntohs(addit[i].resource->type)==1)
  {
   addit[i].rdata = (unsigned char*)malloc(ntohs(addit[i].resource->data_len));
   for(j=0;j<ntohs(addit[i].resource->data_len);j++)
   addit[i].rdata[j]=reader[j];

   addit[i].rdata[ntohs(addit[i].resource->data_len)]='\0';
   reader+=ntohs(addit[i].resource->data_len);
  }
  else
  {
   addit[i].rdata=ReadName(reader,buf,&stop);
   reader+=stop;
  }
 }

 //print answers
 printf("\nAnswer Records : %d \n" , ntohs(dns->ans_count) );
 for(i=0 ; i < ntohs(dns->ans_count) ; i++)
 {
  printf("Name : %s ",answers[i].name);

  if( ntohs(answers[i].resource->type) == T_A) //IPv4 address
  {
   long *p;
   p=(long*)answers[i].rdata;
   a.sin_addr.s_addr=(*p); //working without ntohl
   printf("has IPv4 address : %s",inet_ntoa(a.sin_addr));
  }

  if(ntohs(answers[i].resource->type)==5)
  {
   //Canonical name for an alias
   printf("has alias name : %s",answers[i].rdata);
  }

  printf("\n");
 }

 //print authorities
 printf("\nAuthoritive Records : %d \n" , ntohs(dns->auth_count) );
 for( i=0 ; i < ntohs(dns->auth_count) ; i++)
 {

  printf("Name : %s ",auth[i].name);
  if(ntohs(auth[i].resource->type)==2)
  {
   printf("has nameserver : %s",auth[i].rdata);
  }
  printf("\n");
 }

 //print additional resource records
 printf("\nAdditional Records : %d \n" , ntohs(dns->add_count) );
 for(i=0; i < ntohs(dns->add_count) ; i++)
 {
  printf("Name : %s ",addit[i].name);
  if(ntohs(addit[i].resource->type)==1)
  {
   long *p;
   p=(long*)addit[i].rdata;
   a.sin_addr.s_addr=(*p);
   printf("has IPv4 address : %s",inet_ntoa(a.sin_addr));
  }
  printf("\n");
 }
 return;
}

/*
 *
 * */
u_char* ReadName(unsigned char* reader,unsigned char* buffer,int* count)
{
 unsigned char *name;
 unsigned int p=0,jumped=0,offset;
 int i , j;

 *count = 1;
 name = (unsigned char*)malloc(256);

 name[0]='\0';

 //read the names in 3www6google3com format
 while(*reader!=0)
 {
  if(*reader>=192)
  {
   offset = (*reader)*256 + *(reader+1) - 49152; //49152 = 11000000 00000000 ;)
   reader = buffer + offset - 1;
   jumped = 1; //we have jumped to another location so counting wont go up!
  }
  else
  {
   name[p++]=*reader;
  }

  reader = reader+1;

  if(jumped==0)
  {
   *count = *count + 1; //if we havent jumped to another location then we can count up
  }
 }

 name[p]='\0'; //string complete
 if(jumped==1)
 {
  *count = *count + 1; //number of steps we actually moved forward in the packet
 }

 //now convert 3www6google3com0 to www.google.com
 for(i=0;i<(int)strlen((const char*)name);i++)
 {
  p=name[i];
  for(j=0;j<(int)p;j++)
  {
   name[i]=name[i+1];
   i=i+1;
  }
  name[i]='.';
 }
 name[i-1]='\0'; //remove the last dot
 return name;
}

/*
 * Get the DNS servers from /etc/resolv.conf file on Linux
 * */
void get_dns_servers()
{
 FILE *fp;
 char line[200] , *p;
 if((fp = fopen("/etc/resolv.conf" , "r")) == NULL)
 {
  printf("Failed opening /etc/resolv.conf file \n");
 }

 while(fgets(line , 200 , fp))
 {
  if(line[0] == '#')
  {
   continue;
  }
  if(strncmp(line , "nameserver" , 10) == 0)
  {
   p = strtok(line , " ");
   p = strtok(NULL , " ");

   //p now is the dns ip :)
   //????
  }
 }

 strcpy(dns_servers[0] , "208.67.222.222");
 strcpy(dns_servers[1] , "208.67.220.220");
}

/*
 * This will convert www.google.com to 3www6google3com
 * got it :)
 * */
void ChangetoDnsNameFormat(unsigned char* dns,unsigned char* host)
{
 int lock = 0 , i;
 strcat((char*)host,".");

 for(i = 0 ; i < strlen((char*)host) ; i++)
 {
  if(host[i]=='.')
  {
   *dns++ = i-lock;
   for(;lock<i;lock++)
   {
    *dns++=host[lock];
   }
   lock++; //or lock=i+1;
  }
 }
 *dns++='\0';
}

dns.c

复制代码 代码如下:

/*
 * DNS Query Program on Linux
 *
 * Author : ismdeep@live.com
 *
 * */
//Header Files
#include<stdio.h> //printf
#include<string.h> //strlen
#include<stdlib.h> //malloc
#include<sys/socket.h> //you know what this is for
#include<arpa/inet.h> //inet_addr , inet_ntoa , ntohs etc
#include<netinet/in.h>
#include<unistd.h> //getpid

//List of DNS Servers registered on the system
char dns_servers[10][100];
int dns_server_count = 0;
//Types of DNS resource records :)

#define T_A 1 //Ipv4 address
#define T_NS 2 //Nameserver
#define T_CNAME 5 // canonical name
#define T_SOA 6 /* start of authority zone */
#define T_PTR 12 /* domain name pointer */
#define T_MX 15 //Mail server

//Function Prototypes
void ngethostbyname (unsigned char* , int);
void ChangetoDnsNameFormat (unsigned char*,unsigned char*);
unsigned char* ReadName (unsigned char*,unsigned char*,int*);
void get_dns_servers();

//DNS header structure
struct DNS_HEADER
{
 unsigned short id; // identification number

 unsigned char rd :1; // recursion desired
 unsigned char tc :1; // truncated message
 unsigned char aa :1; // authoritive answer
 unsigned char opcode :4; // purpose of message
 unsigned char qr :1; // query/response flag

 unsigned char rcode :4; // response code
 unsigned char cd :1; // checking disabled
 unsigned char ad :1; // authenticated data
 unsigned char z :1; // its z! reserved
 unsigned char ra :1; // recursion available

 unsigned short q_count; // number of question entries
 unsigned short ans_count; // number of answer entries
 unsigned short auth_count; // number of authority entries
 unsigned short add_count; // number of resource entries
};

//Constant sized fields of query structure
struct QUESTION
{
 unsigned short qtype;
 unsigned short qclass;
};

//Constant sized fields of the resource record structure
#pragma pack(push, 1)
struct R_DATA
{
 unsigned short type;
 unsigned short _class;
 unsigned int ttl;
 unsigned short data_len;
};
#pragma pack(pop)

//Pointers to resource record contents
struct RES_RECORD
{
 unsigned char *name;
 struct R_DATA *resource;
 unsigned char *rdata;
};

//Structure of a Query
typedef struct
{
 unsigned char *name;
 struct QUESTION *ques;
} QUERY;

int main( int argc , char *argv[])
{
 unsigned char hostname[100];

 //Get the DNS servers from the resolv.conf file
 get_dns_servers();

 //Get the hostname from the terminal
 printf("Enter Hostname to Lookup : ");
 scanf("%s" , hostname);

 //Now get the ip of this hostname , A record
 ngethostbyname(hostname , T_A);

 return 0;
}

/*
 * Perform a DNS query by sending a packet
 * */
void ngethostbyname(unsigned char *host , int query_type)
{
 unsigned char buf[65536],*qname,*reader;
 int i , j , stop , s;

 struct sockaddr_in a;

 struct RES_RECORD answers[20],auth[20],addit[20]; //the replies from the DNS server
 struct sockaddr_in dest;

 struct DNS_HEADER *dns = NULL;
 struct QUESTION *qinfo = NULL;

 printf("Resolving %s" , host);

 s = socket(AF_INET , SOCK_DGRAM , IPPROTO_UDP); //UDP packet for DNS queries

 dest.sin_family = AF_INET;
 dest.sin_port = htons(53);
 dest.sin_addr.s_addr = inet_addr(dns_servers[0]); //dns servers

 //Set the DNS structure to standard queries
 dns = (struct DNS_HEADER *)&buf;

 dns->id = (unsigned short) htons(getpid());
 dns->qr = 0; //This is a query
 dns->opcode = 0; //This is a standard query
 dns->aa = 0; //Not Authoritative
 dns->tc = 0; //This message is not truncated
 dns->rd = 1; //Recursion Desired
 dns->ra = 0; //Recursion not available! hey we dont have it (lol)
 dns->z = 0;
 dns->ad = 0;
 dns->cd = 0;
 dns->rcode = 0;
 dns->q_count = htons(1); //we have only 1 question
 dns->ans_count = 0;
 dns->auth_count = 0;
 dns->add_count = 0;

 //point to the query portion
 qname =(unsigned char*)&buf[sizeof(struct DNS_HEADER)];

 ChangetoDnsNameFormat(qname , host);
 qinfo =(struct QUESTION*)&buf[sizeof(struct DNS_HEADER) + (strlen((const char*)qname) + 1)]; //fill it

 qinfo->qtype = htons( query_type ); //type of the query , A , MX , CNAME , NS etc
 qinfo->qclass = htons(1); //its internet (lol)

 printf("\nSending Packet...");
 if( sendto(s,(char*)buf,sizeof(struct DNS_HEADER) + (strlen((const char*)qname)+1) + sizeof(struct QUESTION),0,(struct sockaddr*)&dest,sizeof(dest)) < 0)
 {
  perror("sendto failed");
 }
 printf("Done");

 //Receive the answer
 i = sizeof dest;
 printf("\nReceiving answer...");
 if(recvfrom (s,(char*)buf , 65536 , 0 , (struct sockaddr*)&dest , (socklen_t*)&i ) < 0)
 {
  perror("recvfrom failed");
 }
 printf("Done");

 dns = (struct DNS_HEADER*) buf;

 //move ahead of the dns header and the query field
 reader = &buf[sizeof(struct DNS_HEADER) + (strlen((const char*)qname)+1) + sizeof(struct QUESTION)];

 printf("\nThe response contains : ");
 printf("\n %d Questions.",ntohs(dns->q_count));
 printf("\n %d Answers.",ntohs(dns->ans_count));
 printf("\n %d Authoritative Servers.",ntohs(dns->auth_count));
 printf("\n %d Additional records.\n\n",ntohs(dns->add_count));

 //Start reading answers
 stop=0;

 for(i=0;i<ntohs(dns->ans_count);i++)
 {
  answers[i].name=ReadName(reader,buf,&stop);
  reader = reader + stop;

  answers[i].resource = (struct R_DATA*)(reader);
  reader = reader + sizeof(struct R_DATA);

  if(ntohs(answers[i].resource->type) == 1) //if its an ipv4 address
  {
   answers[i].rdata = (unsigned char*)malloc(ntohs(answers[i].resource->data_len));

   for(j=0 ; j<ntohs(answers[i].resource->data_len) ; j++)
   {
    answers[i].rdata[j]=reader[j];
   }

   answers[i].rdata[ntohs(answers[i].resource->data_len)] = '\0';

   reader = reader + ntohs(answers[i].resource->data_len);
  }
  else
  {
   answers[i].rdata = ReadName(reader,buf,&stop);
   reader = reader + stop;
  }
 }

 //read authorities
 for(i=0;i<ntohs(dns->auth_count);i++)
 {
  auth[i].name=ReadName(reader,buf,&stop);
  reader+=stop;

  auth[i].resource=(struct R_DATA*)(reader);
  reader+=sizeof(struct R_DATA);

  auth[i].rdata=ReadName(reader,buf,&stop);
  reader+=stop;
 }

 //read additional
 for(i=0;i<ntohs(dns->add_count);i++)
 {
  addit[i].name=ReadName(reader,buf,&stop);
  reader+=stop;

  addit[i].resource=(struct R_DATA*)(reader);
  reader+=sizeof(struct R_DATA);

  if(ntohs(addit[i].resource->type)==1)
  {
   addit[i].rdata = (unsigned char*)malloc(ntohs(addit[i].resource->data_len));
   for(j=0;j<ntohs(addit[i].resource->data_len);j++)
   addit[i].rdata[j]=reader[j];

   addit[i].rdata[ntohs(addit[i].resource->data_len)]='\0';
   reader+=ntohs(addit[i].resource->data_len);
  }
  else
  {
   addit[i].rdata=ReadName(reader,buf,&stop);
   reader+=stop;
  }
 }

 //print answers
 printf("\nAnswer Records : %d \n" , ntohs(dns->ans_count) );
 for(i=0 ; i < ntohs(dns->ans_count) ; i++)
 {
  printf("Name : %s ",answers[i].name);

  if( ntohs(answers[i].resource->type) == T_A) //IPv4 address
  {
   long *p;
   p=(long*)answers[i].rdata;
   a.sin_addr.s_addr=(*p); //working without ntohl
   printf("has IPv4 address : %s",inet_ntoa(a.sin_addr));
  }

  if(ntohs(answers[i].resource->type)==5)
  {
   //Canonical name for an alias
   printf("has alias name : %s",answers[i].rdata);
  }

  printf("\n");
 }

 //print authorities
 printf("\nAuthoritive Records : %d \n" , ntohs(dns->auth_count) );
 for( i=0 ; i < ntohs(dns->auth_count) ; i++)
 {

  printf("Name : %s ",auth[i].name);
  if(ntohs(auth[i].resource->type)==2)
  {
   printf("has nameserver : %s",auth[i].rdata);
  }
  printf("\n");
 }

 //print additional resource records
 printf("\nAdditional Records : %d \n" , ntohs(dns->add_count) );
 for(i=0; i < ntohs(dns->add_count) ; i++)
 {
  printf("Name : %s ",addit[i].name);
  if(ntohs(addit[i].resource->type)==1)
  {
   long *p;
   p=(long*)addit[i].rdata;
   a.sin_addr.s_addr=(*p);
   printf("has IPv4 address : %s",inet_ntoa(a.sin_addr));
  }
  printf("\n");
 }
 return;
}

/*
 *
 * */
u_char* ReadName(unsigned char* reader,unsigned char* buffer,int* count)
{
 unsigned char *name;
 unsigned int p=0,jumped=0,offset;
 int i , j;

 *count = 1;
 name = (unsigned char*)malloc(256);

 name[0]='\0';

 //read the names in 3www6google3com format
 while(*reader!=0)
 {
  if(*reader>=192)
  {
   offset = (*reader)*256 + *(reader+1) - 49152; //49152 = 11000000 00000000 ;)
   reader = buffer + offset - 1;
   jumped = 1; //we have jumped to another location so counting wont go up!
  }
  else
  {
   name[p++]=*reader;
  }

  reader = reader+1;

  if(jumped==0)
  {
   *count = *count + 1; //if we havent jumped to another location then we can count up
  }
 }

 name[p]='\0'; //string complete
 if(jumped==1)
 {
  *count = *count + 1; //number of steps we actually moved forward in the packet
 }

 //now convert 3www6google3com0 to www.google.com
 for(i=0;i<(int)strlen((const char*)name);i++)
 {
  p=name[i];
  for(j=0;j<(int)p;j++)
  {
   name[i]=name[i+1];
   i=i+1;
  }
  name[i]='.';
 }
 name[i-1]='\0'; //remove the last dot
 return name;
}

/*
 * Get the DNS servers from /etc/resolv.conf file on Linux
 * */
void get_dns_servers()
{
 FILE *fp;
 char line[200] , *p;
 if((fp = fopen("/etc/resolv.conf" , "r")) == NULL)
 {
  printf("Failed opening /etc/resolv.conf file \n");
 }

 while(fgets(line , 200 , fp))
 {
  if(line[0] == '#')
  {
   continue;
  }
  if(strncmp(line , "nameserver" , 10) == 0)
  {
   p = strtok(line , " ");
   p = strtok(NULL , " ");

   //p now is the dns ip :)
   //????
  }
 }

 strcpy(dns_servers[0] , "208.67.222.222");
 strcpy(dns_servers[1] , "208.67.220.220");
}

/*
 * This will convert www.google.com to 3www6google3com
 * got it :)
 * */
void ChangetoDnsNameFormat(unsigned char* dns,unsigned char* host)
{
 int lock = 0 , i;
 strcat((char*)host,".");

 for(i = 0 ; i < strlen((char*)host) ; i++)
 {
  if(host[i]=='.')
  {
   *dns++ = i-lock;
   for(;lock<i;lock++)
   {
    *dns++=host[lock];
   }
   lock++; //or lock=i+1;
  }
 }
 *dns++='\0';
}

时间: 2024-07-31 21:30:21

linux中查询dns示例_C 语言的相关文章

二分查找算法在C/C++程序中的应用示例_C 语言

 二分查找算法的思想很简单,<编程珠玑>中的描述: 在一个包含t的数组内,二分查找通过对范围的跟综来解决问题.开始时,范围就是整个数组.通过将范围中间的元素与t比较并丢弃一半范围,范围就被缩小.这个过程一直持续,直到在t被发现,或者那个能够包含t的范围已成为空.         Donald Knuth在他的<Sorting and Searching>一书中指出,尽管第一个二分查找算法早在1946年就被发表,但第一个没有bug的二分查找算法却是在12年后才被发表出来.其中常见的一

C++如何实现DNS域名解析_C 语言

一.概述 现在来搞定DNS域名解析,其实这是前面一篇文章C++实现Ping里面的遗留问题,要干的活是ping的过程中画红线的部分: cmd下域名解析的命令是nslookup,比如"nslookup www.baidu.com"的结果如下: 其中,Address返回的就是www.baidu.com对应的IP地址,这个可能有多个 Alias指别名,也就是说www.baidu.com是www.a.shifen.com的别名,而www.a.shifen.com则是www.baidu.com的规

详细解析C语言中的开方实现_C 语言

关于C语言中的开方计算,首先想到的当然是sqrt()函数,让我们先来回顾一下它的基本用法: 头文件:#include <math.h> sqrt() 用来求给定值的平方根,其原型为: double sqrt(double x); 参数 x 为要计算平方根的值. 如果 x < 0,将会导致 domain error 错误,并把全局变量 errno 的值为设置为 EDOM. 返回值 返回 x 平方根. 注意,使用 GCC 编译时请加入-lm. 实例计算200 的平方根值. #include

Linux网络编程之基于UDP实现可靠的文件传输示例_C 语言

了解网络传输协议的人都知道,采用TCP实现文件传输很简单.相对于TCP,由于UDP是面向无连接.不可靠的传输协议,所以我们需要考虑丢包和后发先至(包的顺序)的问题,所以我们想要实现UDP传输文件,则需要解决这两个问题.方法就是给数据包编号,按照包的顺序接收并存储,接收端接收到数据包后发送确认信息给发送端,发送端接收确认数据以后再继续发送下一个包,如果接收端收到的数据包的编号不是期望的编号,则要求发送端重新发送. 下面展示的是基于linux下C语言实现的一个示例程序,该程序定义一个包的结构体,其中

C语言编程中借助pthreads库进行多线程编程的示例_C 语言

运行之前需要做一些配置: 1.下载PTHREAD的WINDOWS开发包 pthreads-w32-2-4-0-release.exe(任何一个版本均可)    http://sourceware.org/pthreads-win32/ ,解压到一个目录. 2.找到include和lib文件夹,下面分别把它们添加到VC++6.0的头文件路径和静态链接库路径下面:    a).Tools->Options,选择Directory页面,然后在Show directories for:中选择Includ

Linux网络编程之socket文件传输示例_C 语言

本文所述示例程序是基于Linux平台的socket网络编程,实现文件传输功能.该示例是基于TCP流协议实现的socket网络文件传输程序.采用C语言编写.最终能够实现传输任何格式文件的文件传输程序. 具体实现代码如下: Server端代码如下: /************************************************************************* > File Name: Server.c > Author: SongLee ***********

通过C++程序示例理解设计模式中的外观模式_C 语言

举一个生活中的小例子,大凡开过学或者毕过业的都会体会到这样一种郁闷:你要去 n个地方办理 n 个手续(现在大学合并后就更加麻烦,因为可能那 n 个地方都隔的比较远). 但是实际上我们需要的就是一个最后一道手续的证明而已,对于前面的手续是怎么办的.到什么地方去办理我们都不感兴趣. 实际上在软件系统开发中也经常回会遇到这样的情况,可能你实现了一些接口(模块),而这些接口(模块)都分布在几个类中(比如 A 和 B.C.D):A 中实现了一些接口,B 中实现一些接口(或者 A 代表一个独立模块,B.C.

C++中关于委派(Delegates)的实现示例_C 语言

 介绍 在 C++ 中通过一个全局函数来绑定到对象的成员函数是很有用的,这个特性也存在于其他语言中,例如 C#的委派.在 C++ 中相当于成员函数指针,但是 并没有提供相应的特性.在这篇文章中,我想提出一个简单的 C++ 委派的实现,是用 C++ 成员函数指针和 C++11 的可变模板(variadic templates),目前这套实现方法仅支持 GNU C++ 4.7.0,在 Windows 下可使用 MinGW. 背景 在我的方法中奖提供一个create_delegate函数,可通过下面两

Cocos2d-x中实现弹出对话框示例_C 语言

在游戏中我们经常会看到弹出一个对话框让我们进行选择,今天我们就在cocos2dx中实现这个对话框.对话框说白了也是一个层,当我们点击某一个按钮的时候这个层被加进了当前的场景中,同时场景中的其他的层都是不可点击的,这个时候就涉及到触摸的优先级的一些问题,当然有些时候你也可以根据自己的需要让其他的层也可以点击,但是道理都是一样的,学会了这个其他的按照自己的要求去实现吧.下面我将弹出层单独分装成一个类,供我们调用. /*对话框场景类的头文件*/ #ifndef _POP_SCENE_H_ #defin