2012年1月19日 星期四

Get mac address from IP in winsock

reference: http://vcpptips.wordpress.com/tag/gethostname/
reference: http://www.binarytides.com/blog/get-mac-address-from-ip-in-winsock/


  //--------------------------------------------------------//
  // Get Host IP Addresses                                                //
  //--------------------------------------------------------//
  char szHost[256]="";
  ::gethostname(szHost,256);
  hostent *pHost = ::gethostbyname(szHost);
  for(BYTE i=0; ;i++)
  {
    //Get mac addresses of the ip
    struct in_addr srcip;
    srcip.S_un = ((struct in_addr *)(pHost->h_addr))->S_un;
    unsigned char mac[6];
    GetMacAddress(mac , srcip);
    //Get host name
    char *p = pHost->h_addr_list[i];
    BYTE host_name[200];
    memset(host_name, 0x00, 200);

    if(p ==NULL)
      break;
  }