How to search for computers on a local area network

Source: Internet
Author: User
Tags bool

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;


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.