본문 바로가기

TCP/IP

inet_ntoa() 함수

/*인터넷 주소를 표시할 수 있는 3가지 방식중, 32비트 IP주소 -> dotted decimal  할 때,
inet_ntoa()함수를 사용한 프로그램*/

#include
<stdio.h>
#include<string.h>

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>

int main(int argc, char *argv[])
{
  char *haddr;
  struct in_addr host_ip;
  if(argc<2)
  {
    printf("How to Use : %s IP address(dotted decimal)\n", argv[0]);
    exit(0);
  }

  haddr = argv[1];  //dotted decimal address
  host_ip.s_addr = inet_addr(haddr);

  /*IP address hexa 4bytes output*/
  printf("IP Address (hexa) : 0x%x\n", host_ip.s_addr);

  /*IP address : hexa 4bytes -> dotted decimal*/
  printf("dotted decimal Address : %s\n", inet_ntoa(host_ip));

  return 0;
}



'TCP/IP' 카테고리의 다른 글

inet_addr()함수, (=inet_aton)  (0) 2011.07.01
호스트pc(리틀엔디안) <-> 네트워크(빅엔디안)  (0) 2011.07.01
gethostbyname() 함수  (0) 2011.07.01
소켓의 생성과 해지  (0) 2011.06.29
네트워크 프로그래밍 실습  (0) 2011.06.28