我沒有得到有關查找機器IP地址的任務。我需要幫助理解這段代碼的邏輯。我們的大學實驗室使用代理服務此代碼是否可以在沒有代理的計算機上工作?需要幫助瞭解C程序,找到機器IP地址
#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);
}
你讀過正在使用的函數的manpages嗎?那之後你不瞭解什麼部分? – Thomas 2010-07-25 17:46:42