UDP通信绑定本地端口IP问题!!!!! 我想程序在绑定端口IP的时候能自动获取本机的IP,不知道该如何实现??是不是有封装好的获得本机IP的函数??求解!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 附:演示程序源代码// IpHelper.cpp : Defines the entry point for the console application.// #include "stdafx.h"#include "windows.h"#include "iostream.h"#include "iphlpapi.h" typedef DWORD(CALLBACK * PGNOINTERFACE)(PDWORD);//GetNumberOfInterfacestypedef DWORD(CALLBACK * PGAINFO)(PIP_ADAPTER_INFO,PULONG);//GetAdaptersInfotypedef DWORD(CALLBACK * PGIINFO)(PIP_INTERFACE_INFO,PULONG );//GetInterfaceInfotypedef DWORD(CALLBACK * PGIAT)(PMIB_IPADDRTABLE,PULONG,BOOL);//GetIpAddrTabletypedef DWORD(CALLBACK * PAIA)(IPAddr,IPMask,DWORD,PULONG,PULONG);//AddIPAddress int main(int argc, char* argv[]){ DWORD index=0; //函数指针 PGAINFO pGAInfo; //加载IP Helper API 所需的库文件 HINSTANCE hInst;//实例句柄 hInst=LoadLibrary("iphlpapi.dll"); if(!hInst) cout<<"iphlpapi.dll not supported in this platform!\n"; cout<<"net adapters information:"<<endl<<endl; //------------------------------------》获得网卡数据 pGAInfo=(PGAINFO)GetProcAddress(hInst,"GetAdaptersInfo"); PIP_ADAPTER_INFO pInfo=NULL,pInfoTemp=NULL; ULONG ulSize=0; pGAInfo(pInfo,&ulSize);//第一次调用,获取缓冲区大小 pInfoTemp=pInfo=(PIP_ADAPTER_INFO)new(char[ulSize]); pGAInfo(pInfo,&ulSize); //遍历每一张网卡 while(pInfo) { //网卡名 cout<<"adapter name: "<<pInfo->AdapterName<<endl; //网卡描述信息 cout<<"description: "<<pInfo->Description<<endl; //记录下它的索引值以便后面使用。 index=pInfo->Index; //物理地址的长度 cout<<"hardware address length: "<<pInfo->AddressLength<<endl; //显示物理地址 cout<<"hardware address:"; cout.setf(ios::hex); cout.unsetf(ios::dec); for(int i=0;i<(int)pInfo->AddressLength;i++) cout<<(unsigned int)pInfo->Address[i]; cout<<endl; cout.unsetf(ios::hex); cout.setf(ios::dec); //显示绑定于这张网卡之上的IP地址 cout<<"ip address bound to this chapter: "<<endl; PIP_ADDR_STRING pAddTemp=&(pInfo->IpAddressList); while(pAddTemp)/*遍历IP列表中的每一个元素*/ { cout<<pAddTemp->IpAddress.String<<endl; pAddTemp=pAddTemp->Next; } //显示当前使用的IP地址 //因为在离线状态下pInfo->CurrentIpAddress被置空,所以有必要做检查 if(pInfo->CurrentIpAddress) { cout<<"current ip using:"<<endl; pAddTemp=pInfo->CurrentIpAddress; while(pAddTemp)/*遍历IP列表中的每一个元素*/ { cout<<pAddTemp->IpAddress.String<<endl; pAddTemp=pAddTemp->Next; } } else cout<<"network malfunctioning,no ip is in use!\n"<<endl; //显示DHCP 服务器数据 cout<<"DHCP in use:"<<endl; pAddTemp=&(pInfo->DhcpServer); while(pAddTemp)/*遍历IP列表中的每一个元素*/ { cout<<pAddTemp->IpAddress.String<<endl; pAddTemp=pAddTemp->Next; } //将当前指针移向下一个 pInfo=pInfo->Next; } delete pInfoTemp;//回收无用内存 //------------------------------------》获得网卡数据部分结束 cout<<endl<<endl; //函数指针 PGNOINTERFACE pGNOInterface; PGIINFO pGIInfo; cout<<"network interfaces information:"<<endl<<endl; //------------------------------------》网络接口信息部分 //显示网络接口的个数 DWORD ulNumOfInterfaces=0; pGNOInterface=(PGNOINTERFACE)GetProcAddress(hInst,"GetNumberOfInterfaces"); pGNOInterface(&ulNumOfInterfaces); cout<<"U have "<<ulNumOfInterfaces<<" network interfaces\n"; //获取网络接口信息 pGIInfo=(PGIINFO)GetProcAddress(hInst,"GetInterfaceInfo"); PIP_INTERFACE_INFO pIInfo=NULL; ulSize=0; pGIInfo(pIInfo,&ulSize);//第一次调用,获取缓冲区大小 pIInfo=(PIP_INTERFACE_INFO)new(char[ulSize]); pGIInfo(pIInfo,&ulSize); //显示网络接口信息 for(int i=0;i<pIInfo->NumAdapters;i++) { cout<<"Adapter index:"<<pIInfo->Adapter[i].Index<<endl; cout<<"and name:"<<pIInfo->Adapter[i].Name<<endl; } delete pIInfo; //------------------------------------》网络接口信息部分结束 cout<<endl<<endl; //函数指针 PGIAT pGIAT; cout<<"ip information:"<<endl<<endl; //------------------------------------》IP绑定信息 pGIAT=(PGIAT)GetProcAddress(hInst,"GetIpAddrTable"); PMIB_IPADDRTABLE pIPTable=NULL; ulSize=0; pGIAT(pIPTable,&ulSize,TRUE);//获得缓冲区大小 pIPTable=(PMIB_IPADDRTABLE)new(char[ulSize]); pGIAT(pIPTable,&ulSize,TRUE); for(i=0;i<pIPTable->dwNumEntries;i++) { //取出每一个字段,显示IP cout<<"ip address:"<<(unsigned int)((LOWORD(pIPTable->table[i].dwAddr)&0x00FF))<<"."\ <<(unsigned int)((LOWORD(pIPTable->table[i].dwAddr)>>8))<<"."\ <<(unsigned int)((HIWORD(pIPTable->table[i].dwAddr)&0x00FF))<<"."\ <<(unsigned int)((HIWORD(pIPTable->table[i].dwAddr))>>8)<<endl; //显示绑定网络接口的索引 cout<<"it is bound to interface:"<<pIPTable->table[i].dwIndex<<endl; //显示子网掩码 cout<<"it's net mask:"<<(unsigned int)((LOWORD(pIPTable->table[i].dwMask)&0x00FF))<<"."\ <<(unsigned int)((LOWORD(pIPTable->table[i].dwMask)>>8))<<"."\ <<(unsigned int)((HIWORD(pIPTable->table[i].dwMask)&0x00FF))<<"."\ <<(unsigned int)((HIWORD(pIPTable->table[i].dwMask))>>16)<<endl; //显示广播地址 cout<<"and broadcast addres:"<<(unsigned int)((LOWORD(pIPTable->table[i].dwBCastAddr)&0x00FF))<<"."\ <<(unsigned int)((LOWORD(pIPTable->table[i].dwBCastAddr)>>8))<<"."\ <<(unsigned int)((HIWORD(pIPTable->table[i].dwBCastAddr)&0x00FF))<<"."\ <<(unsigned int)((HIWORD(pIPTable->table[i].dwBCastAddr))>>16)<<endl; //显示最大报文大小 cout<<"it's reassembly size:"<<pIPTable->table[i].dwReasmSize<<endl; } //------------------------------------》IP绑定信息结束 PAIA pAIA; //------------------------------------》IP设置部分 if(!index) { cout<<"no adapters available, cannot set ip address!"<<endl; return 0;//没有网络接口可以设置 } pAIA=(PAIA)GetProcAddress(hInst,"AddIPAddress"); IPAddr addr=0x184BC5CA; IPMask mask=0x00FFFFFF; ULONG context; ULONG Inst; pAIA(addr,mask,index,&context,&Inst); //------------------------------------》IP设置部分结束 cout<<endl<<endl; return 0;} 在扩展屏幕上显示虚拟桌面。 有些迷惑,希望大家能帮出主意 CFile Write后Read的问题 如何依次得到数组中的每个字符 一个C++中的很弱智的问题,绝对送分 [求助]请教一个多字段表的插入及更新问题 带tab页浏览器的一些问题 急求,关于WriteFile&ReadFile通信超时的问题, 在线等待 我要用vc通过odbc的crecordset类来随机的调用access数据库中满足某一条件的数据,应该怎样做? 各位,有知道如何用四面体构造三维图像的吗? 请教一个sql查询语句 对话框 系统菜单
// IpHelper.cpp : Defines the entry point for the console application.// #include "stdafx.h"#include "windows.h"#include "iostream.h"#include "iphlpapi.h" typedef DWORD(CALLBACK * PGNOINTERFACE)(PDWORD);//GetNumberOfInterfacestypedef DWORD(CALLBACK * PGAINFO)(PIP_ADAPTER_INFO,PULONG);//GetAdaptersInfotypedef DWORD(CALLBACK * PGIINFO)(PIP_INTERFACE_INFO,PULONG );//GetInterfaceInfotypedef DWORD(CALLBACK * PGIAT)(PMIB_IPADDRTABLE,PULONG,BOOL);//GetIpAddrTabletypedef DWORD(CALLBACK * PAIA)(IPAddr,IPMask,DWORD,PULONG,PULONG);//AddIPAddress int main(int argc, char* argv[]){ DWORD index=0; //函数指针 PGAINFO pGAInfo; //加载IP Helper API 所需的库文件 HINSTANCE hInst;//实例句柄 hInst=LoadLibrary("iphlpapi.dll"); if(!hInst) cout<<"iphlpapi.dll not supported in this platform!\n"; cout<<"net adapters information:"<<endl<<endl; //------------------------------------》获得网卡数据 pGAInfo=(PGAINFO)GetProcAddress(hInst,"GetAdaptersInfo"); PIP_ADAPTER_INFO pInfo=NULL,pInfoTemp=NULL; ULONG ulSize=0; pGAInfo(pInfo,&ulSize);//第一次调用,获取缓冲区大小 pInfoTemp=pInfo=(PIP_ADAPTER_INFO)new(char[ulSize]); pGAInfo(pInfo,&ulSize); //遍历每一张网卡 while(pInfo) { //网卡名 cout<<"adapter name: "<<pInfo->AdapterName<<endl; //网卡描述信息 cout<<"description: "<<pInfo->Description<<endl; //记录下它的索引值以便后面使用。 index=pInfo->Index; //物理地址的长度 cout<<"hardware address length: "<<pInfo->AddressLength<<endl; //显示物理地址 cout<<"hardware address:"; cout.setf(ios::hex); cout.unsetf(ios::dec); for(int i=0;i<(int)pInfo->AddressLength;i++) cout<<(unsigned int)pInfo->Address[i]; cout<<endl; cout.unsetf(ios::hex); cout.setf(ios::dec); //显示绑定于这张网卡之上的IP地址 cout<<"ip address bound to this chapter: "<<endl; PIP_ADDR_STRING pAddTemp=&(pInfo->IpAddressList); while(pAddTemp)/*遍历IP列表中的每一个元素*/ { cout<<pAddTemp->IpAddress.String<<endl; pAddTemp=pAddTemp->Next; } //显示当前使用的IP地址 //因为在离线状态下pInfo->CurrentIpAddress被置空,所以有必要做检查 if(pInfo->CurrentIpAddress) { cout<<"current ip using:"<<endl; pAddTemp=pInfo->CurrentIpAddress; while(pAddTemp)/*遍历IP列表中的每一个元素*/ { cout<<pAddTemp->IpAddress.String<<endl; pAddTemp=pAddTemp->Next; } } else cout<<"network malfunctioning,no ip is in use!\n"<<endl; //显示DHCP 服务器数据 cout<<"DHCP in use:"<<endl; pAddTemp=&(pInfo->DhcpServer); while(pAddTemp)/*遍历IP列表中的每一个元素*/ { cout<<pAddTemp->IpAddress.String<<endl; pAddTemp=pAddTemp->Next; } //将当前指针移向下一个 pInfo=pInfo->Next; } delete pInfoTemp;//回收无用内存 //------------------------------------》获得网卡数据部分结束 cout<<endl<<endl; //函数指针 PGNOINTERFACE pGNOInterface; PGIINFO pGIInfo; cout<<"network interfaces information:"<<endl<<endl; //------------------------------------》网络接口信息部分 //显示网络接口的个数 DWORD ulNumOfInterfaces=0; pGNOInterface=(PGNOINTERFACE)GetProcAddress(hInst,"GetNumberOfInterfaces"); pGNOInterface(&ulNumOfInterfaces); cout<<"U have "<<ulNumOfInterfaces<<" network interfaces\n"; //获取网络接口信息 pGIInfo=(PGIINFO)GetProcAddress(hInst,"GetInterfaceInfo"); PIP_INTERFACE_INFO pIInfo=NULL; ulSize=0; pGIInfo(pIInfo,&ulSize);//第一次调用,获取缓冲区大小 pIInfo=(PIP_INTERFACE_INFO)new(char[ulSize]); pGIInfo(pIInfo,&ulSize); //显示网络接口信息 for(int i=0;i<pIInfo->NumAdapters;i++) { cout<<"Adapter index:"<<pIInfo->Adapter[i].Index<<endl; cout<<"and name:"<<pIInfo->Adapter[i].Name<<endl; } delete pIInfo; //------------------------------------》网络接口信息部分结束 cout<<endl<<endl; //函数指针 PGIAT pGIAT; cout<<"ip information:"<<endl<<endl; //------------------------------------》IP绑定信息 pGIAT=(PGIAT)GetProcAddress(hInst,"GetIpAddrTable"); PMIB_IPADDRTABLE pIPTable=NULL; ulSize=0; pGIAT(pIPTable,&ulSize,TRUE);//获得缓冲区大小 pIPTable=(PMIB_IPADDRTABLE)new(char[ulSize]); pGIAT(pIPTable,&ulSize,TRUE); for(i=0;i<pIPTable->dwNumEntries;i++) { //取出每一个字段,显示IP cout<<"ip address:"<<(unsigned int)((LOWORD(pIPTable->table[i].dwAddr)&0x00FF))<<"."\ <<(unsigned int)((LOWORD(pIPTable->table[i].dwAddr)>>8))<<"."\ <<(unsigned int)((HIWORD(pIPTable->table[i].dwAddr)&0x00FF))<<"."\ <<(unsigned int)((HIWORD(pIPTable->table[i].dwAddr))>>8)<<endl; //显示绑定网络接口的索引 cout<<"it is bound to interface:"<<pIPTable->table[i].dwIndex<<endl; //显示子网掩码 cout<<"it's net mask:"<<(unsigned int)((LOWORD(pIPTable->table[i].dwMask)&0x00FF))<<"."\ <<(unsigned int)((LOWORD(pIPTable->table[i].dwMask)>>8))<<"."\ <<(unsigned int)((HIWORD(pIPTable->table[i].dwMask)&0x00FF))<<"."\ <<(unsigned int)((HIWORD(pIPTable->table[i].dwMask))>>16)<<endl; //显示广播地址 cout<<"and broadcast addres:"<<(unsigned int)((LOWORD(pIPTable->table[i].dwBCastAddr)&0x00FF))<<"."\ <<(unsigned int)((LOWORD(pIPTable->table[i].dwBCastAddr)>>8))<<"."\ <<(unsigned int)((HIWORD(pIPTable->table[i].dwBCastAddr)&0x00FF))<<"."\ <<(unsigned int)((HIWORD(pIPTable->table[i].dwBCastAddr))>>16)<<endl; //显示最大报文大小 cout<<"it's reassembly size:"<<pIPTable->table[i].dwReasmSize<<endl; } //------------------------------------》IP绑定信息结束 PAIA pAIA; //------------------------------------》IP设置部分 if(!index) { cout<<"no adapters available, cannot set ip address!"<<endl; return 0;//没有网络接口可以设置 } pAIA=(PAIA)GetProcAddress(hInst,"AddIPAddress"); IPAddr addr=0x184BC5CA; IPMask mask=0x00FFFFFF; ULONG context; ULONG Inst; pAIA(addr,mask,index,&context,&Inst); //------------------------------------》IP设置部分结束 cout<<endl<<endl; return 0;}