ÃÑ ÆäÀÌÁö ¼ö : 3224

Àüü ÇÔ¼ö/¿ë¾î»çÀü
Facebook Joinc ±×·ì   Joinc QA »çÀÌÆ®
ÇöÀçÀ§Ä¡ : Code>C++>linux_getMyIP



joinc´Â Firefox¿Í chrome¿¡¼­ Å×½ºÆ® Çß½À´Ï´Ù. IE¿¡¼­´Â Å×À̺íÀÌ ±úÁö°Å³ª À̹ÌÁö°¡ º¸ÀÌÁö ¾ÊÀ» ¼ö ÀÖ½À´Ï´Ù. ƯÈ÷ ±¸±Û DocsÀ̹ÌÁöÀÇ °æ¿ì ¿¢¹Úó¸®µÉ ¼ö ÀÖ½À´Ï´Ù.

  1. linux_getMyIP
  2. ¸®´ª½º Àü¿ë ÀÚ½ÅÀÇ IP ¾ò¾î¿À´Â ÇÔ¼ö
  3. purewell
  4. Version 1.0
  5. 2004/11/05


¼³¸í

¸®´ª½º Àü¿ë ÀÚ½ÅÀÇ IP¸¦ ¾ò¾î¿À´Â ÇÔ¼öÀÔ´Ï´Ù.

»ç¿ë¹æ¹ý

¼Ò½º ÄÚµå ¾È¿¡ 'todo' ÄÚ¸àÆ® ÀÖÀ½

ÄÚµå

 //------------------------------------------------------------------------------ 
// 
// Get machine ip addresses by sockaddr for sockaddr_in/sockaddr_in6. 
// 
// AUTHOR  : Yubin Lim 
// DATE    : 2004-11-05 
// EMAIL   : purewell at purewell dot biz 
// REQUIRE : Linux, 
//                  GNU C/C++ Compiler 
// 
//------------------------------------------------------------------------------ 
 
 
# include <sys/types.h> 
# include <sys/socket.h> 
# include <sys/select.h> 
# include <sys/ioctl.h> 
# include <netdb.h> 
# include <netinet/in.h> 
# include <netinet/tcp.h> 
# include <arpa/inet.h> 
# include <pthread.h> 
# include <unistd.h> 
# include <sys/epoll.h> 
# include <net/if.h> 
# include <linux/sockios.h> 
 
static bool linux_getMyIP(int nFamily) 
{ 
  int             sock; 
  int             nRet; 
 
  size_t          nNIC; 
  const size_t    nMaxNIC = 256; 
 
  struct ifconf   ifc; 
  struct ifreq    ifr[nMaxNIC]; 
 
  struct sockaddr* pAddr(NULL); 
  struct sockaddr_in addr4; 
  struct sockaddr_in6 addr6; 
 
  sock = socket(nFamily, SOCK_STREAM, 0); 
  if ( sock == -1 ) return false; 
 
  ifc.ifc_len = sizeof(ifr); 
  ifc.ifc_ifcu.ifcu_req = ifr; 
 
  nRet = ioctl(sock, SIOCGIFCONF, &ifc); 
  if ( nRet == -1) return false; 
 
  close(sock); 
 
  nNIC = ifc.ifc_len / sizeof(struct ifreq); 
 
  addr_buf.nIPVersion = nFamily; 
 
  for ( size_t i = 0 ; i < nNIC; i ++ ) 
  { 
    if ( nFamily == ifc.ifc_ifcu.ifcu_req[i].ifr_ifru.ifru_addr.sa_family ) 
    { 
      // todo convert binary to string... 
      // if nFamily is AF_INET then pAddr is struct sockaddr_in*. 
      // else if nFamily is AF_INET6 then pAddr is struct sockaddr_in6*. 
      pAddr = (&ifc.ifc_ifcu.ifcu_req[i].ifr_ifru.ifru_addr); 
    } 
  } 
 
  return true; 
} 
 

º¯°æ»çÇ×


2004/01/08

EmailÀ» ±âÀÔÇϸé, ´ñ±ÛÀÌ ¸ÞÀÏ·Î Àü´ÞµË´Ï´Ù.