Tôi đang sử dụng đoạn mã này tôi tìm thấy trong http://www.kutukupret.com/2009/09/28/gethostbyname-vs-getaddrinfo/ để thực hiện tra cứu dnsCó vấn đề gì với gethostbyname?
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char *argv[ ]) {
struct hostent *h;
/* error check the command line */
if(argc != 2) {
fprintf(stderr, "Usage: %s hostname\n", argv[0]);
exit(1);
}
/* get the host info */
if((h=gethostbyname(argv[1])) == NULL) {
herror("gethostbyname(): ");
exit(1);
}
else
printf("Hostname: %s\n", h->h_name);
printf("IP Address: %s\n", inet_ntoa(*((struct in_addr *)h->h_addr)));
return 0;
}
Tôi đang phải đối mặt với một thực tế lạ
./test www.google.com
Hostname: www.l.google.com
IP Address: 209.85.148.103
hoạt động tốt, nhưng nếu tôi cố gắng giải quyết một địa chỉ IP không đầy đủ tôi có được điều này
./test 10.1.1
Hostname: 10.1.1
IP Address: 10.1.0.1
tôi mong chờ một lỗi như sau
./test www.google
gethostbyname(): : Unknown host
nhưng chương trình có vẻ hoạt động.
Bất kỳ ý tưởng nào tại sao?
Tôi vừa thử mã từ http://en.wikipedia.org/wiki/Getaddrinfo cho getaddrinfo, nhưng 10.1.1 sill giải quyết trong 10.1.0.1 ... Có phải là lỗi không? Tui bỏ lỡ điều gì vậy? –