본문 바로가기

Development/C/C++

Network - Socket - Get Local IP Address




Sample Source - Get Ip Address



char   szIpBuffer[30];

struct ifreq       ifr;

struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;

int    sfd;


printf("[ %s ] Start Get IP interface eth0\n", __FUNCTION__);


memset(&ifr, 0, sizeof(struct ifreq));

memset(szIpBuffer, 0x00, sizeof(szIpBuffer));


strcpy(ifr.ifr_name, "eth0");

sin->sin_family = AF_INET;


if (0 > (sfd = socket(AF_INET, SOCK_STREAM, 0)))

{

printf("socket()...error\n");

return;

}


if (0==ioctl(sfd, SIOCGIFADDR, &ifr))

{

strcpy(szIpBuffer, inet_ntoa(sin->sin_addr));

printf("[%s] IP is %s\n", __FUNCTION__, szIpBuffer);

}

else

printf("[%s] Error: Get eth0 Ip Address \n", __FUNCTION__);


close(sfd); 


'Development > C/C++' 카테고리의 다른 글

popen()  (0) 2015.11.02
Linux C/C++ Error Code 확인방법  (0) 2015.10.30