본문 바로가기

과제모음

네트워크프로그래밍 - 상대방의 접속정보를 알아내는 프로그램

반응형
#include <netdb.h>
#include <sys/socket.h>
#include <stdio.h>

int main(){
struct in_addr addr;
struct hostent *host;
const char *hostName;
int i;
printf("input url : ");
hostName = (char *)malloc(sizeof(char));          // 동적메모리 할당

scanf("%s",hostName);                                 //문자열을 받음


if((host = gethostbyname(hostName)) == NULL){
printf("gethostbyname() error - check network\n");
exit(-1);
}

printf("official name = %s\n", host->h_name);

i = 0;
while(host->h_aliases[i] != NULL){
printf("aliases = %s\n", host->h_aliases[i++]);
}

printf("address type = %d\n", host->h_addrtype);
printf("address length = %d\n", host->h_length);

i = 0;
while(host->h_addr_list[i] != NULL){
memcpy(&addr.s_addr, host->h_addr_list[i], 4);
printf("address = %s(0x%x)\n", inet_ntoa(addr), ntohl(*(long*)host->h_addr_list[i]));
i++;
}

return 0;
}

반응형