奈何搜索局域网上的计较机
当前位置:以往代写 > C/C++ 教程 >奈何搜索局域网上的计较机
2019-06-13

奈何搜索局域网上的计较机

奈何搜索局域网上的计较机

在一个局域网上,网络编程IP相关问题

1.怎么搜索局域网上的计较机?

2.奈何通过一个计较机的IP地点得到其网卡物理地点?

譬喻,我的计较机为A,局域网上的有别的一台计较机为B,奈何在A上通过B的IP地点获取B的网卡物理地点?

如何编程用C++Builder编程实现?

//------------来自JiShiPing(JSP季世平)的代码:---------------
#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 resources
  lpNR, // pointer to resource structure (NULL at first 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;
}
//------获取某主机的IP地点,假如主机名为空,则返回本机的名和IP地点---------
//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= "无法获取 ";
    return IP;
   }
   else
   IP=inet_ntoa(*(struct in_addr*)hp- >h_addr_list[0]);
   }
  catch(...)
  {
   IP= " ";
  }
  WSACleanup();
  return IP;
}

    关键字:

在线提交作业