在一个局域网上,网络编程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
|