如何得到本局域网上所有主机的ip?

ljfppp 2002-09-02 06:18:52
如果使用VC得到本局域网上所有主机的ip?谢谢

等到花儿再开时
...全文
62 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljfppp 2002-09-05
  • 打赏
  • 举报
回复
老大,还是不行啊,如果对于访问需要密码的主机,gethostbyname()fanh返回的照样是NULL,而且用WNetEnumResource()速度很慢,有没有解决的方法啊?谢谢了,救急啊
ljfppp 2002-09-04
  • 打赏
  • 举报
回复
打字打错了,不是i can 是ican't.虽然用kingzai的方法可以得到其他机器的机器名,但用gethostbyname()想要得到其他机器的信息时,返回的总是NULL,呵呵那就得不到其他机器的IP了,有什么办法解决吗?是不是网络上的主机不准匿名访问就不能用gethostbyname()得到它的其他信息那?谢谢诶
Lemon_2000 2002-09-04
  • 打赏
  • 举报
回复
up
mjk 2002-09-04
  • 打赏
  • 举报
回复
没错
kingzai 2002-09-04
  • 打赏
  • 举报
回复
oh,you should init socket.these code may help you:
/从主机名得到IP地址,需要你先配置好域名服务器
int GetPacketListView::GetIPAddress(const CString& sHostName, CString& sIPAddress)
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return 0;
}
struct hostent *lpHostEnt = gethostbyname (sHostName);
if (lpHostEnt == NULL)
{
// An error occurred.
sIPAddress = _T("");
return WSAGetLastError();
}

LPSTR lpAddr = lpHostEnt->h_addr_list[0];
if (lpAddr)
{
struct in_addr inAddr;
memmove (&inAddr, lpAddr, 4);
sIPAddress = inet_ntoa (inAddr);
if (sIPAddress.IsEmpty())
sIPAddress = _T("Not available");
}
WSACleanup();
return 1;
}

ljfppp 2002-09-04
  • 打赏
  • 举报
回复
thand to kingzai,my problem almost be resolved.But there is still a small problem:i can get others'computer by call founction gethostbyname();The return value is NULL.When I access the same computer in Network Neighbor,there is a dialog pop up,which ask the account and password.My operation system is win200.So how can i get all IPs on my LAN? 3x
ljfppp 2002-09-03
  • 打赏
  • 举报
回复
用GetProcAddress()后,返回的是NULL,why?
kingzai 2002-09-03
  • 打赏
  • 举报
回复
why do you call GetProcAddress?
or you can select this method,consult this article:
http://www.codeproject.com/internet/ipaddress.asp
nuaawyd 2002-09-02
  • 打赏
  • 举报
回复
下面的应该有用
http://www.vchelp.net/vchelp/archive.asp?type_id=39&class_id=1&cata_id=5&article_id=653
dxhdxh2k 2002-09-02
  • 打赏
  • 举报
回复
up
kingzai 2002-09-02
  • 打赏
  • 举报
回复
oh,sorry,it is a custom struct by user,you can disabe these lines and rebuild it.
hfycl 2002-09-02
  • 打赏
  • 举报
回复
关注
ljfppp 2002-09-02
  • 打赏
  • 举报
回复
pcinfo是什么结构啊?谢谢
kingzai 2002-09-02
  • 打赏
  • 举报
回复
typedef struct _NETRESOURCE {
DWORD dwScope;
DWORD dwType;
DWORD dwDisplayType;
DWORD dwUsage;
LPTSTR lpLocalName;
LPTSTR lpRemoteName;
LPTSTR lpComment;
LPTSTR lpProvider;
} NETRESOURCE;
//struct point
ljfppp 2002-09-02
  • 打赏
  • 举报
回复
LPNETRESOURCE是什么冬冬啊?谢谢指教
nuaawyd 2002-09-02
  • 打赏
  • 举报
回复
(李二)你不要要求太高好不好

楼主的方法已经搞定了
ornot 2002-09-02
  • 打赏
  • 举报
回复
我想上面的代码只是针对WINDOWS主机吧?如是LINUX主机就不行了吧?


UPUP!!
kingzai 2002-09-02
  • 打赏
  • 举报
回复
BOOL CIPWnd::GetLanComputers(LPNETRESOURCE lpnr,HTREEITEM hNode)
{
DWORD dwResult,dwResultEnum,dwDsptype,dwIP;
DWORD cbBuffer = 16384; /* 16K is reasonable size */
DWORD cEntries = 0xFFFFFFFF; /* enumerate all possible entries */
LPNETRESOURCE lpnrLocal; /* pointer to enumerated structures */
char * ptr;
pcinfo * pPcinfo;
struct hostent FAR * pHostent;
HANDLE hEnum;
dwResult=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,0,lpnr,&hEnum);
if (dwResult ==67) //domain下没有可访问的主机
return TRUE;
if (dwResult != NO_ERROR )
return FALSE;
{
lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);
dwResultEnum = WNetEnumResource(hEnum, /* resource handle */
¢ries, /* defined locally as 0xFFFFFFFF */
lpnrLocal, /* LPNETRESOURCE */
&cbBuffer); /* buffer size */
if (dwResultEnum == NO_ERROR)
{
if (!g_pThread) return FALSE;
for(DWORD i = 0; i < cEntries; i++)
{
if (!g_pThread) return FALSE;
dwDsptype=(lpnrLocal+i)->dwDisplayType;
// RESOURCEDISPLAYTYPE_DOMAIN 0x00000001
// RESOURCEDISPLAYTYPE_SERVER 0x00000002
// RESOURCEDISPLAYTYPE_GROUP 0x00000005
// RESOURCEDISPLAYTYPE_NETWORK 0x00000006

if (dwDsptype==1||dwDsptype==2||dwDsptype==5||dwDsptype==6)
{
m_treeview.SendMessage(TVM_EXPAND,TVE_EXPAND,(long)hNode);
if (dwDsptype==2)
{
ptr=(lpnrLocal+i)->lpRemoteName+2;
pHostent = gethostbyname(ptr);
if (!g_pThread) return FALSE;
if (pHostent)
{
memcpy(&dwIP, pHostent->h_addr_list[0], pHostent->h_length);
pPcinfo=new pcinfo;
pPcinfo->uIP=dwIP;
pPcinfo->iflag=0;
pPcinfo->totalBytes=0;
pPcinfo->totalPacks=0;
ZeroMemory(pPcinfo->szName,MAX_HOSTNAME_LAN);
memcpy(pPcinfo->szName,ptr,strlen(ptr));
m_pcArr.Add(pPcinfo);
}
else
dwIP=0;

}
else
{
ptr=(lpnrLocal+i)->lpRemoteName;
dwIP=0;
}
HTREEITEM hNode1=m_treeview.InsertItem(ptr,hNode);
m_treeview.SetItemData(hNode1,dwIP);
if(RESOURCEUSAGE_CONTAINER == ((lpnrLocal+i)->dwUsage & RESOURCEUSAGE_CONTAINER) && (dwDsptype==1||dwDsptype==5||dwDsptype==6))
{
m_treeview.SendMessage(TVM_EXPAND,TVE_EXPAND,(long)hNode1);
GetLanComputers((lpnrLocal+i),hNode1);
}
}
}
}

GlobalFree((HGLOBAL) lpnrLocal);
}

WNetCloseEnum(hEnum);
return TRUE;
}

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧