I have a client server program using UPD created in MFC. In the server there are two listboxes. The first one displays the ip addresses of the connected clients.
I want the second listbox to display the MAC address when i click the ip address from the listbox.
So how can I display the MAC address of the client by clicking the ip address in the first listbox?
also would it be better to use the ip address control the toolbox in visual studios provides or is a listbox ok?
The MAC address, by TCP/IP standards, is never communicated outside of the local-area network to which it pertains -- routers beyond that LAN don't even get the information you're trying to record.
CBuffer result;
IP_ADAPTER_INFO adapter_info[16]; // Allocate information
// for up to 16 NICs
DWORD buf_len = sizeof(adapter_info); // Save memory size of buffer
const DWORD status = GetAdaptersInfo( // Call Getadapter_info
adapter_info, // [out] buffer to receive data
&buf_len); // [in] size of receive data buffer
if(ERROR_SUCCESS == status) // Verify return value is
{ // valid, no buffer overflow
for(PIP_ADAPTER_INFO ai = adapter_info; ai != null; ai = ai->Next)
{
if(MIB_IF_TYPE_ETHERNET == ai->Type)
{ // At this point you know it's an ethernet adapter. Now you can check if it's the ip-address you want via 'CurrentIpAddress'
result.SetData(ai->Address, ai->AddressLength);
break;
}
}
}
return result;
So how can I display the MAC address of the client by clicking the ip address in the first listbox?
also would it be better to use the ip address control the toolbox in visual studios provides or is a listbox ok?
This depends on how your GUI is organized. You may also consider a combobox
i asked 2 questions, sorry if i wasn't clear enough.
yes i want to get the MAC address - thanks for the code
then i want to display it in the second listbox when i click the ip address from the first list box.
This depends on how your GUI is organized
what do you mean by this?
if listbox is not a good option then should a use a textbox or ip address control?
IP_ADAPTER_INFO AdapterInfo[16]; // Allocate information for up to 16 NICs
DWORD dwBufLen = sizeof(AdapterInfo); // Save the memory size of buffer
DWORD dwStatus = GetAdaptersInfo( // Call GetAdapterInfo
AdapterInfo, // [out] buffer to receive data
&dwBufLen); // [in] size of receive data buffer
assert(dwStatus == ERROR_SUCCESS); // Verify return value is valid, no buffer overflow
PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;// Contains pointer to current adapter info
do {
PrintMACaddress(pAdapterInfo->Address); // Print MAC address
pAdapterInfo = pAdapterInfo->Next; // Progress through linked list
}
while(pAdapterInfo); // Terminate if last adapter
}
and i just put this code in a button event handler (just for testing)
now what do i do to make the mac address appear when i click an ip address from the first listbox?
also when i press the button it displays the current computer's mac address so how do i change it so get the mac of the ip address i click?
What you need is to find the MAC address that correlates with the ip address. Please follow the link above. You will find certain members of the IP_ADAPTER_INFO struct. There's also an additional example.
now what do i do to make the mac address appear when i click an ip address from the first listbox?
You need to compare the selected ip address from your ip listbox with the ip address(es) from the pAdapterInfo. If you have a match you add the MAC address to the MAC listbox
also when i press the button it displays the current computer's mac address so how do i change it so get the mac of the ip address i click?
It's nearly the same as getting the ip address, jut compare the MAC addresses and you have the ip addresses.
bool GetMACFromIP(BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH], const std::string &selected_ip_adr)
{
IP_ADAPTER_INFO AdapterInfo[16]; // Allocate information for up to 16 NICs
DWORD dwBufLen = sizeof(AdapterInfo); // Save the memory size of buffer
DWORD dwStatus = GetAdaptersInfo( // Call GetAdapterInfo
AdapterInfo, // [out] buffer to receive data
&dwBufLen); // [in] size of receive data buffer
assert(dwStatus == ERROR_SUCCESS); // Verify return value is valid, no buffer overflow
PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;// Contains pointer to current adapter info
bool found = false;
do {
const IP_ADDR_STRING *addr_str = &pAdapterInfo->IpAddressList; // use the address list instead of CurrentIpAddress (which is often NULL)
while(addr_str != null)
{
if(selected_ip_adr == addr_str->IpAddress.String) // This compares the selected ip address with an associated Adapter ip address
{
found = true;
break;
}
}
if(found)
{
memcpy(Address, pAdapterInfo->Address, MAX_ADAPTER_ADDRESS_LENGTH); // copy the adapters MAC address
break;
}
else
pAdapterInfo = pAdapterInfo->Next; // Progress through linked list
}
while(pAdapterInfo); // Terminate if last adapter
return found; // This determines whether the provided ip address is associated with a MAC address
}
ok i put bool GetMACFromIP(BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH], const std::string &selected_ip_adr); into the list box event handler.
but i need to put the line PrintMACaddress(pAdapterInfo->Address); into the code so it prints the MAC address but i get the compile error error C3861: 'PrintMACaddress': identifier not found
this is the print MAC function and it worked in the old code so i don't know why its causing a problem now
bool CmfcServerDlg::GetMACFromIP(BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH], const std::string &selected_ip_adr)
{
IP_ADAPTER_INFO AdapterInfo[16]; // Allocate information for up to 16 NICs
DWORD dwBufLen = sizeof(AdapterInfo); // Save the memory size of buffer
DWORD dwStatus = GetAdaptersInfo( // Call GetAdapterInfo
AdapterInfo, // [out] buffer to receive data
&dwBufLen); // [in] size of receive data buffer
assert(dwStatus == ERROR_SUCCESS); // Verify return value is valid, no buffer overflow
PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;// Contains pointer to current adapter info
bool found = false;
do {
const IP_ADDR_STRING *addr_str = &pAdapterInfo->IpAddressList; // use the address list instead of CurrentIpAddress (which is often NULL)
while(addr_str != NULL)
{
if(selected_ip_adr == addr_str->IpAddress.String) // This compares the selected ip address with an associated Adapter ip address
{
found = true;
break;
}
}
if(found)
{
memcpy(Address, pAdapterInfo->Address, MAX_ADAPTER_ADDRESS_LENGTH); // copy the adapters MAC address
PrintMACaddress(pAdapterInfo->Address);
break;
}
else
pAdapterInfo = pAdapterInfo->Next; // Progress through linked list
PrintMACaddress(pAdapterInfo->Address);
}
while(pAdapterInfo); // Terminate if last adapter
return found; // This determines whether the provided ip address is associated with a MAC address
}
So did you check if PrintMACaddress works at all? MFC is often tricky to use.
Also the provided ip address might not exists within the adapter.
This would be good time to start debugging...