On a LAN, network programming IP related issues
1. How to search the computer on the local area network?
2. How to obtain the physical address of its network card through the IP address of a computer?
For example, my computer is a, there is another computer on the LAN for B, how to get B's network card physical address on a B's IP address?
How to program with C++builder programming to achieve?
//------------code from jishiping (JSP Di Shiping):---------------
#include <winnetwk.h >
#include <winsock2.h >
//---------------------------------------------------------------------------
__fastcall Tform1::tform1 (tcomponent* Owner)
: Tform (Owner)
{
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall tform1::formcreate (tobject *sender)
{
TStrings *netlist=new tstringlist ();
Tlistitem *newitem;
//
listview1->items->clear ();
screen->Cursor=crHourGlass;
//
Enumnetresource (Netlist,null,resource_globalnet,resourcetype_any);
for (int i=0;i <netlist->count;i++)
{
newitem=listview1->items->add ();
newitem->caption=netlist->Strings[i];
newitem->subitems->add (Gethostip (netlist->strings[i));
}
screen->Cursor=crDefault;
Delete netlist;
}
//---------------------------------------------------------------------------
bool __fastcall tform1::enumnetresource (TStrings *rclist,lpnetresource lpnr, DWORD dwscope,dword dwType)
{
HANDLE henum = 0; Resource-handle
DWORD dwresult = WNetOpenEnum (
Dwscope,//Scope of enumeration
dwtype,//resource types to list
0,//Enumerate all
LPNR,//Pointer to resource structure (NULL at a-time)
&henum//Handle to resource
) ;
if (dwresult!= no_error) return false;
bool Bret=true;
DWORD dwentries = 0xFFFFFFFF; Enumerate all possible entries
Netresource nr[1024];
DWORD dwbuffer=1024*sizeof (Netresource);
while (1)
{
dwresult = WNetEnumResource (Henum,&dwentries, (LPVOID) nr,&dwbuffer);
if (dwresult = = Error_no_more_items) break;
else if (dwresult!= no_error) {bret=false; break;}
for (DWORD i = 0; i < dwentries; i++)
{
if (nr[i].dwdisplaytype==resourcedisplaytype_server)
{
Char *p=nr[i].lpremotename;
while (*p== ' \ ") p++;
if (*p) rclist->add (p);
}
else if ((Nr[i].dwusage&resourceusage_container) ==resourceusage_container)
{
Bret=enumnetresource (Rclist,&nr[i],dwscope,dwtype);
if (bret==false) break;
}
}
if (bret==false) break;
}
Wnetcloseenum (Henum);
return bRet;
}
//------Obtain the IP address of a host, and if the host name is empty, return the name and IP address of this computer---------
//ansistring Gethostip (ansistring &host)
ansistring __fastcall tform1::gethostip (ansistring &host)
{
Wsadata Wsadata;
ansistring IP;
WSAStartup (Makeword (2,0), &wsadata);
if (host. IsEmpty ())
{
Char hostname[128];
if (gethostname (hostname,128)!=0) return ansistring ("");
Host=hostname;
}
Try
{
struct hostent *hp=gethostbyname (Host.c_str ());
if (WSAGetLastError () ==wsahost_not_found)
{
ip= "Unable to obtain";
return IP;
}
Else
Ip=inet_ntoa (* (struct in_addr*) hp->h_addr_list[0]);
}
catch (...)
{
ip= "";
}
WSACleanup ();
return IP;
}