- 论坛徽章:
- 0
|
各位,请原谅我发这里,因为在C那里我得不到确定,希望前辈人物指点。
下面的小程序是一个解析IP地址的,我刚接触Socket编程,那个unsigned long是因为hostent表中的结构是32位的吗?
- #include <stdio.h> /* stderr, stdout */
- #include <netdb.h> /* hostent struct, gethostbyname() */
- #include <arpa/inet.h> /* inet_ntoa() to format IP address */
- #include <netinet/in.h> /* in_addr structure */
- int main(int argc, char **argv) {
- struct hostent *host; /* host information */
- struct in_addr h_addr; /* Internet address */
- if (argc != 2) {
- fprintf(stderr, "USAGE: nslookup <inet_address>\n");
- exit(1);
- }
- if ((host = gethostbyname(argv[1])) == NULL) {
- fprintf(stderr, "(mini) nslookup failed on '%s'\n", argv[1]);
- exit(1);
- }
- h_addr.s_addr = *((unsigned long *) host->h_addr_list[0]);
- fprintf(stdout, "%s\n", inet_ntoa(h_addr));
- exit(0);
- }
复制代码 |
|