리눅스 도메인을 ip주소로 변환하기 쌩초보 도움요청

dltjfry12의 이미지

도와주세요. 저는 리눅스라는 건 단 1도 모르고 살았던 사람인데요,

학교 과제로 이런 걸 받았는데,(사진 첨부)

제가 어찌어찌 구글링과 유튜브로 우분투를 가상머신에 설치하고

vim eee.c 를 해서 eee라는 파일은 만드는데 성공했어요.

터미널도 열었구요 gcc -o eee eee.c 이렇게 해서 실행하는 법도 아는데

도저히 저 코드를 못 짜겠어요.. 도와주세요..

File attachments: 
첨부파일 크기
Image icon 캡처.PNG272.39 KB
익명 사용자의 이미지

학교 과제라는 걸 너무 노골적으로 드러냈다는 생각이 들지 않으시나요?

김정균의 이미지

구글로 달려가서 "gethostbyaddr 예제" 로 검색해 보세요.

익명 사용자의 이미지

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
 
void error_handling(const char* msg) {
        puts(msg);
}
 
int main(void) {
        struct hostent *hostData;
        const char *hnames[] = {"kubernetes.io", "namu.wiki", "filejo.com"};
        struct in_addr address[10];
 
        puts("Call gethostbyname function");
 
        for(int i = 0, j = 0; i < (int)(sizeof(hnames) / sizeof(hnames[0])); i++) {
                hostData = gethostbyname(hnames[i]);
                if(hostData == NULL)
                        error_handling("gethostbyname error!");
                else
                        while(hostData->h_addr != NULL) {
                                address[j] = *(struct in_addr *) hostData->h_addr;
 
                                printf("%s : ", hostData->h_name);
                                printf("%s\n", inet_ntoa(address[j]));
 
                                hostData->h_addr_list++;
                                j++;
                        }
        }
 
        puts("\nCall gethostbyaddr function");
 
        for(int i = 0; i < 10; i++) {
                hostData = gethostbyaddr((const char *)&address[i], sizeof(address[i]), AF_INET);
                printf("%s\n", inet_ntoa(address[i]));
                if(hostData == NULL)
                        error_handling("gethostbyaddr error!");
                else
                        printf("%s\n", hostData->h_name);
        }
 
}