/* ----------------------------------------------------------------------------------------------------------
ÆÄÀϸí : addr_conv.c
±â ´É : ÁÖ¼Òº¯È¯ ÇÁ·Î±×·¥
ÄÄÆÄÀÏ : cc -o addr_conv addr_conv.c -lsocket -lnsl
½ÇÇ࿹ : addr_conv vcn.kangwon.ac.kr 203.252.65.3
--------------------------------------------------------------------------------------------------------------*/
#include <netdb.h>
#include <stdio.h>
#include <string.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 *ptr_he; /* È£½ºÆ®ÀÇ Á¤º¸¸¦ ÀúÀåÇÏ´Â ±¸Á¶Ã¼ */
struct in_addr in; /* ÀÎÅÍ³Ý ÁÖ¼Ò¸¦ ÀúÀåÇÏ´Â ±¸Á¶Ã¼ */
char *hname; /* È£½ºÆ® À̸§ */
char *haddr; /* dotted decimal IP ¹®ÀÚ¿ */
if(argc != 3) {
printf("Usage : %s hostname IP_address\n", argv[0]);
return -1;
}
hname = argv[1];
haddr = argv[2];
/* È£½ºÆ® µµ¸ÞÀÎ ³×ÀÓÀ¸·ÎºÎÅÍ È£½ºÆ® Á¤º¸ ±¸Çϱâ */
if((ptr_he = gethostbyname(hname))) {
memcpy(&in.s_addr,ptr_he->h_addr_list[0], sizeof(in.s_addr));
printf("%s\'s binary ip address (hexa) : 0x%x\n", hname, in.s_addr);
/* IP ÁÖ¼Ò¸¦ dotted decimal ÇüÅÂÀÇ IP·Î º¯È¯ */
printf("%s\'s dotted decimal IP address : %s\n", hname, inet_ntoa(in));
} else {
printf("gethostbyname error.\n");
}
/* ÀÔ·ÂµÈ dotted decimal IP ÁÖ¼Ò¿¡¼ binary IP ÁÖ¼Ò ±¸Çϱâ */
if((in.s_addr = inet_addr(haddr)) > 0) {
ptr_he = gethostbyaddr((char *)&(in.s_addr), sizeof(in.s_addr), AF_INET);
printf("%s\'s binary IP address (hexa) : 0x%x\n", haddr, in.s_addr);
printf("%s\'s hostname : %s\n", haddr, ptr_he->h_name);
} else {
printf("inet_addr error.\n");
}
}
|