VC如何获取一个机器上的主IP和从IP

z1zhangyanan 2007-11-30 01:59:05
求助,Windows操作系统中,如果有多个IP,那么用VC将如何获取所有的IP呢?
...全文
350 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hljalong 2007-12-05
  • 打赏
  • 举报
回复
我查了一下MSDN,这个函数GetAdaptersAddresses要求有点高:
Client Requires Windows Vista or Windows XP.
Server Requires Windows Server 2008 or Windows Server 2003.
Header Declared in Iphlpapi.h.

Library Use Iphlpapi.lib.

DLL Requires Iphlpapi.dll.

我的VC6是安装到WIN98上的,没有要求的.h和.lib及相应的.dll文件。

我以前是用如下方法做的,当然也是在CSDN上学别人的,你不妨试一下。

char szHostName[128];
CString ipaddr="";

WSADATA WsaData;
WSAStartup(MAKEWORD(2,2), &WsaData);


int rslt = gethostname(szHostName, 128);
if( rslt == 0 )
{
struct hostent * pHost;
pHost = gethostbyname(szHostName);
hostent* mpHost = gethostbyaddr(*(pHost->h_addr_list), 4 ,AF_INET);
for( int i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
{
ipaddr = ipaddr + inet_ntoa (*(struct in_addr *)pHost->h_addr_list[i]) + "\r\n";
}
MessageBox(ipaddr);
}
else
{
int nError = WSAGetLastError();
ipaddr.Format("%d",nError);
MessageBox("出错了:" + ipaddr);
}

WSACleanup();


这个代码只要有winsock2.h和ws2_32.lib就行了。
z1zhangyanan 2007-12-04
  • 打赏
  • 举报
回复
嗯,我用的是VC6.0。这位GG,有没有办法帮忙呀?
hljalong 2007-12-04
  • 打赏
  • 举报
回复
这种情况下,如果你用的是.net环境,只要在代码中的这个类型上点右键,在快捷菜单中就可选择 转到定义 这项,接着你就应该怎么都知道了。
z1zhangyanan 2007-12-04
  • 打赏
  • 举报
回复
这个我已经添加了。而且新建了个工程,单纯添加了这个功能。
但还是报错。错误如下:
D:\vcproject\iptest\iptest.cpp(10) : error C2065: 'PIP_ADAPTER_ADDRESSES' : undeclared identifier
D:\vcproject\iptest\iptest.cpp(10) : error C2146: syntax error : missing ';' before identifier 'pAddresses'
D:\vcproject\iptest\iptest.cpp(10) : error C2065: 'pAddresses' : undeclared identifier
D:\vcproject\iptest\iptest.cpp(11) : error C2065: 'IP_ADAPTER_ADDRESSES' : undeclared identifier
D:\vcproject\iptest\iptest.cpp(11) : error C2059: syntax error : ')'
D:\vcproject\iptest\iptest.cpp(17) : error C2065: 'GetAdaptersAddresses' : undeclared identifier
D:\vcproject\iptest\iptest.cpp(23) : error C2059: syntax error : ')'
D:\vcproject\iptest\iptest.cpp(35) : error C2227: left of '->FriendlyName' must point to class/struct/union
D:\vcproject\iptest\iptest.cpp(36) : error C2227: left of '->Description' must point to class/struct/union
D:\vcproject\iptest\iptest.cpp(37) : error C2227: left of '->Next' must point to class/struct/union

xujianlane 2007-12-04
  • 打赏
  • 举报
回复
#include <winsock2.h>
#include <Ws2tcpip.h>
z1zhangyanan 2007-12-04
  • 打赏
  • 举报
回复
to:xujianlane
我尝试了这段代码,为什么我把winsock.h之类的文件include近来后,还是找不到一些变量和方法呢?比如PIP_ADAPTER_ADDRESSES就找不到啊?
xujianlane 2007-11-30
  • 打赏
  • 举报
回复
有个msn的例子
PIP_ADAPTER_ADDRESSES pAddresses;
pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(sizeof(IP_ADAPTER_ADDRESSES));
ULONG outBufLen = 0;
DWORD dwRetVal = 0;

// Make an initial call to GetAdaptersAddresses to get the
// size needed into the outBufLen variable
if (GetAdaptersAddresses(AF_INET,
0,
NULL,
pAddresses,
&outBufLen) == ERROR_BUFFER_OVERFLOW) {
GlobalFree(pAddresses);
pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(outBufLen);
}

// Make a second call to GetAdapters Addresses to get the
// actual data we want
if ((dwRetVal = GetAdaptersAddresses(AF_INET,
0,
NULL,
pAddresses,
&outBufLen)) == NO_ERROR) {
// If successful, output some information from the data we received
while (pAddresses) {
printf("\tFriendly name: %S\n", pAddresses->FriendlyName);
printf("\tDescription: %S\n", pAddresses->Description);
pAddresses = pAddresses->Next;
}
}
else {
printf("Call to GetAdaptersAddresses failed.\n");
LPVOID lpMsgBuf;
if (FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwRetVal,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL )) {
printf("\tError: %s", lpMsgBuf);
}
LocalFree( lpMsgBuf );
}

z1zhangyanan 2007-11-30
  • 打赏
  • 举报
回复
GetAdaptersAddresses() 怎么用?
有具体的实现吗?谢谢。帮个忙吧。
z1zhangyanan 2007-11-30
  • 打赏
  • 举报
回复
我就是要得到所有的IP地址
xujianlane 2007-11-30
  • 打赏
  • 举报
回复
GetAdaptersAddresses()
不知道你要做什么,这个函数可以获取全部IP,但是会包含进虚拟设备的IP请留意。
飞哥 2007-11-30
  • 打赏
  • 举报
回复
查到的本身就是一个列表,通常取第一个,使用gethostname ,gethostbyname
olfile readme file. [1. 文件名解释] olfile: Offload File 这个工具原本是项目中为测试TOE引擎的效率而设计的, 可以作为socket编程的一个例子来学习。 [2. 文件介绍] 程序中使用socket实现了文件的传输。 在VC6.0中编译通过,目录中olfile.cpp/olfile.h是原文件,可以任意修改,不过请不要改动文件头的作者信息。 有两个目录:server、client,其实编译出来的东西是一样的,当时是为了测试方便才分出来的。 可以直接打开client目录中的工程来编译。 [3. 使用介绍] 程序分client端和server端,server 端监听,client端使用命令行方式发送或接收文件。 在VC6.0中编译生成olfile.exe,使用不同的选项启动client和server。 比如,我们有两台机器10.190.1.1(A), 10.190.1.2(B),加入都运行windows,想把A的c:foo.dat传到B,并放在B的c:foodst.dat, 则需要在B启动server: olfile -server A启动client进行传输: olfile -src c:foo.dat -dst c:foodst.dat -ip 10.190.1.2 -y -src 表示源文件的位置 -dst 表示目标文件的位置 -ip 表示远程机的IP。 -y 表示如果远程机上的目标文件位置原来有文件,则强制覆盖。 可以看出,可以让A当作Server,B当作Client,使用不同的命令行实现上面的文件传递: A启动server: olfile -server B启动client 进行文件传输,这时B的c:foodst.dat是目标: olfile -dst c:foodst.dat -src c:foo.dat -ip 10.190.1.1 -y [4.兼容性] 附带的Makefile文件表明,代码可以在Linux下正常编译。所以,可以运行在Linux下,实现Linux与Windows文件的互传。 可能的命令行会变成: olfile -dst /home/foodst.dat -src c:foo.dat -ip 10.190.1.1 -y [5. 局限和可能存在的问题] 1.程序不会对你所使用的系统产生致命影响,因为它仅仅是一个socket 实例。 2.server 在进行一次文件传输之后就退出,因为程序是单一线程的。 3.可以利用程序的打印输出看一下大概的传输速度(因为这时我当时在项目中写这个工具的原因)。

16,551

社区成员

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

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

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