-2

i have a client server application in MFC using UDP where the server displays the IP address of connected clients in a listbox. If i run the client and server on the same computer the program displays the MAC address but if i try to run the client on a different computer the program crashes. Here are the 3 functions. I have an event handler for the listbox that displays the MAC address in a second listbox when an IP address is selected. PrintMACFromIP is the code for getting the MAC address

void CmfcServerDlg::OnLbnSelchangeListClientaddr()
{
    BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
    int nIndex = m_ClientAddrList.GetCurSel();
    if(nIndex < 0)
        return;

    CString s1;
    m_ClientAddrList.GetText(nIndex, s1);
    PrintMACFromIP(s1);

}

void CmfcServerDlg::PrintMACaddress(unsigned char MACData[])
{
    CString strText;
    strText.Format("%02X-%02X-%02X-%02X-%02X-%02X\n",MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);
    m_ClientIdList.AddString(strText); 
}


void CmfcServerDlg:: PrintMACFromIP(const CString &selected_ip_adr)
{
    IP_ADAPTER_INFO AdapterInfo[16];            
    DWORD dwBufLen = sizeof(AdapterInfo);       

    DWORD dwStatus = GetAdaptersInfo(           
        AdapterInfo,                            
        &dwBufLen);                         
    assert(dwStatus == ERROR_SUCCESS);      

    PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
    bool found = false;
    do {
        const IP_ADDR_STRING *addr_str = &pAdapterInfo->IpAddressList;
        while(addr_str != NULL)
        {

          if(selected_ip_adr == addr_str->IpAddress.String) 
          { 
            found = true;
            break;
          }
        }
        if(found)
        {
          PrintMACaddress(pAdapterInfo->Address);
          break;
        }
        else
        {
            pAdapterInfo = pAdapterInfo->Next;      
        }
    }
    while(pAdapterInfo);                        
}
Lisa Collins
  • 101
  • 3
  • 13
  • Have you tried putting debug statements or tracing it down through an IDE? It more than likely is due to the use of a null pointer but you haven't provided enough information such as dumped core or stacktraces. You should also check if `pAdapterInfo` is NULL before using it. –  Mar 26 '13 at 12:05
  • i debugged it and found it gets stuck in the while loop. so is there something wrong with the code? – Lisa Collins Mar 26 '13 at 12:11
  • Where in the while loop? –  Mar 26 '13 at 12:13
  • it jumps out of the while loop when it reaches if(selected_ip_adr == addr_str->IpAddress.String) so found is never set to true – Lisa Collins Mar 26 '13 at 12:15
  • That's normal, if (selected_ip_adr == addr_str->IpAddress.String) if false then you will loop forever because addr_str never changes. – Jabberwocky Mar 26 '13 at 12:39
  • yes but what do i need to change in the code to be able to find the MAC address of another computer? this line is causing the problem – Lisa Collins Mar 26 '13 at 12:43
  • You are probably just missing a "addr_str = addr_str->Next" at the end of the inner while loop. – Jabberwocky Mar 26 '13 at 12:46
  • @LisaCollins: How are you advancing to the next pAdapterInfo.. think about that. There is a bug in your inner while loop. –  Mar 26 '13 at 12:50
  • @Michael Walz: ok so i put that line just after the if statement and when the program reaches that line it jumps out of the while loop and goes to the else statement. so whats the problem here? why can't it find the MAC address? – Lisa Collins Mar 26 '13 at 12:53
  • @0A0D: how do i fix this bug? – Lisa Collins Mar 26 '13 at 12:54
  • Keep in mind you shouldn't ask other people to debug your code, you should debug it yourself. – sashoalm Mar 26 '13 at 14:04

1 Answers1

0

I believe your bug is here :

while(addr_str != NULL)
{
   if(selected_ip_adr == addr_str->IpAddress.String) 
   { 
      found = true;
      break;
   }
}

Change the while to if (addr_str != NULL)

then

it should look like

if (add_str != NULL)
{
   if (selected_ip_adr == addr_str->IpAddress.String)
   {
      PrintMACaddress(pAdapterInfo->Address);
   }              
}   

pAdapterInfo = pAdapterInfo->Next; 

This should handle if pAdapterInfo is null by using the do/while on the subsequent next calls.

See IP_ADAPTER_INFO structure at MSDN.

  • ok so i ran that code but it hits the break. why isn't it finding the MAC address? – Lisa Collins Mar 26 '13 at 13:00
  • thanks for the code but the problem is still the same. it reaches the if statement and jumps out and executes the else. why won't it execute the if statement? – Lisa Collins Mar 26 '13 at 13:25
  • @LisaCollins: Which if? How many times does the while loop run? –  Mar 26 '13 at 13:39
  • if (selected_ip_adr == addr_str->IpAddress.String). you said remove the while loop – Lisa Collins Mar 26 '13 at 13:42
  • but it still skips the if statement - if (selected_ip_adr == addr_str->IpAddress.String). why won't the program execute the if statement and do the line PrintMACaddress(pAdapterInfo->Address); ? is it still not finding the MAC address? – Lisa Collins Mar 26 '13 at 14:00
  • @LisaCollins: Maybe it really isn't equal.. have you tried debugging that part? –  Mar 26 '13 at 15:09
  • yes i debugged it. like i said the program reaches the line if (selected_ip_adr == addr_str->IpAddress.String) and then jumps to pAdapterInfo = pAdapterInfo->Next; the line inside the if statement is never executed – Lisa Collins Mar 26 '13 at 15:14
  • Then the next pAdapterInfo must be NULL –  Mar 26 '13 at 15:15
  • but why can't it find the mac address of the other computer? i have the ip address – Lisa Collins Mar 26 '13 at 15:18
  • @LisaCollins: Did you even read the MSDN page? "The IP_ADAPTER_INFO structure contains information about a particular network adapter on the local computer." –  Mar 26 '13 at 15:19
  • so i shouldn't use IP_ADAPTER_INFO? my question was how to find the MAC address of another computer – Lisa Collins Mar 26 '13 at 15:20
  • @LisaCollins: Your question was why your program was crashing, which I solved. You need to figure out how to ask a question before coming on here and wasting everyone's time by doing your own research. –  Mar 26 '13 at 15:23
  • no my question was 'how to find the MAC address from IP address c++'. and yes the program crashing was a problem i had but the question in the title is still the problem i'm having – Lisa Collins Mar 26 '13 at 15:28
  • No, that was your title. The crux of your question was here: "but if i try to run the client on a different computer the program crashes". Forget it. I am not wasting anymore of my time. –  Mar 26 '13 at 15:31
  • ok sorry if i wasn't clear enough. i have edited the title question – Lisa Collins Mar 26 '13 at 15:34