#include "stdafx.h" #include <winsock2.h> #include <ws2tcpip.h> #include <iphlpapi.h> #include <stdio.h> #pragma comment(lib, "IPHlpAPI.lib")#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) #define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) /* Note: could also use malloc() and free() */int __cdecl main() { int i; /* Variables used by GetIpAddrTable */ PMIB_IPADDRTABLE pIPAddrTable; DWORD dwSize = 0; DWORD dwRetVal = 0; IN_ADDR IPAddr; /* Variables used to return error message */ LPVOID lpMsgBuf; // Before calling AddIPAddress we use GetIpAddrTable to get // an adapter to which we can add the IP. pIPAddrTable = (MIB_IPADDRTABLE *) MALLOC(sizeof (MIB_IPADDRTABLE)); if (pIPAddrTable) { // Make an initial call to GetIpAddrTable to get the // necessary size into the dwSize variable if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) { FREE(pIPAddrTable); pIPAddrTable = (MIB_IPADDRTABLE *) MALLOC(dwSize); } if (pIPAddrTable == NULL) { printf("Memory allocation failed for GetIpAddrTable\n"); exit(1); } } // Make a second call to GetIpAddrTable to get the // actual data we want if ( (dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 )) != NO_ERROR ) { printf("GetIpAddrTable failed with error %d\n", dwRetVal); 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); } exit(1); } printf("\tNum Entries: %ld\n", pIPAddrTable->dwNumEntries); for (i=0; i < (int) pIPAddrTable->dwNumEntries; i++) { printf("\n\tInterface Index[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwIndex); IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwAddr; printf("\tIP Address[%d]: \t%s\n", i, inet_ntoa(IPAddr) ); IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwMask; printf("\tSubnet Mask[%d]: \t%s\n", i, inet_ntoa(IPAddr) ); IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwBCastAddr; printf("\tBroadCast[%d]: \t%s (%ld%)\n", i, inet_ntoa(IPAddr), pIPAddrTable->table[i].dwBCastAddr); printf("\tReassembly size[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwReasmSize); printf("\tType and State[%d]:", i); if (pIPAddrTable->table[i].wType & MIB_IPADDR_PRIMARY) printf("\tPrimary IP Address"); if (pIPAddrTable->table[i].wType & MIB_IPADDR_DYNAMIC) printf("\tDynamic IP Address"); if (pIPAddrTable->table[i].wType & MIB_IPADDR_DISCONNECTED) printf("\tAddress is on disconnected interface"); if (pIPAddrTable->table[i].wType & MIB_IPADDR_DELETED) printf("\tAddress is being deleted"); if (pIPAddrTable->table[i].wType & MIB_IPADDR_TRANSIENT) printf("\tTransient address"); printf("\n"); } if (pIPAddrTable) { FREE(pIPAddrTable); pIPAddrTable = NULL; } exit(0); }
用广播吧,简单。 另外,别获取IP地址,多网卡的时候你就乱了。自己做个配置文件存放你的IP地址。
TO Eleven (用户名:VisualEleven):我看了下下面获取IP的方法说明 http://blog.csdn.net/kakaxia6337/archive/2010/08/24/5835875.aspx里面只用到pIPAddrTable->table[0]的ip,您为什么选择循环查询?是因为可能有多个网卡吗? 我说下自己理解,请指正下。dwAddr表示本机地址,dwMask表示子网掩码,dwIndex不知道,dwBCastAddr表示广播地址,dwReasmSize不知道。 如果我可以确定只有一张网卡,是否就能用pIPAddrTable->table[0]来获取?
还列了具体计算方法
一般是 子网掩码 与 IP地址 进行位与运算,得处网络地址 ,
网络地址 | (~子网掩码),得出广播地址
谁能举个例子计算广播地址
另外获取本机IP和子网掩码的函数是什么呢?请大伙帮忙解答下,谢谢!!
#include "stdafx.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "IPHlpAPI.lib")#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
/* Note: could also use malloc() and free() */int __cdecl main()
{ int i; /* Variables used by GetIpAddrTable */
PMIB_IPADDRTABLE pIPAddrTable;
DWORD dwSize = 0;
DWORD dwRetVal = 0;
IN_ADDR IPAddr; /* Variables used to return error message */
LPVOID lpMsgBuf; // Before calling AddIPAddress we use GetIpAddrTable to get
// an adapter to which we can add the IP.
pIPAddrTable = (MIB_IPADDRTABLE *) MALLOC(sizeof (MIB_IPADDRTABLE)); if (pIPAddrTable) {
// Make an initial call to GetIpAddrTable to get the
// necessary size into the dwSize variable
if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) ==
ERROR_INSUFFICIENT_BUFFER) {
FREE(pIPAddrTable);
pIPAddrTable = (MIB_IPADDRTABLE *) MALLOC(dwSize); }
if (pIPAddrTable == NULL) {
printf("Memory allocation failed for GetIpAddrTable\n");
exit(1);
}
}
// Make a second call to GetIpAddrTable to get the
// actual data we want
if ( (dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 )) != NO_ERROR ) {
printf("GetIpAddrTable failed with error %d\n", dwRetVal);
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);
}
exit(1);
} printf("\tNum Entries: %ld\n", pIPAddrTable->dwNumEntries);
for (i=0; i < (int) pIPAddrTable->dwNumEntries; i++) {
printf("\n\tInterface Index[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwIndex);
IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwAddr;
printf("\tIP Address[%d]: \t%s\n", i, inet_ntoa(IPAddr) );
IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwMask;
printf("\tSubnet Mask[%d]: \t%s\n", i, inet_ntoa(IPAddr) );
IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwBCastAddr;
printf("\tBroadCast[%d]: \t%s (%ld%)\n", i, inet_ntoa(IPAddr), pIPAddrTable->table[i].dwBCastAddr);
printf("\tReassembly size[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwReasmSize);
printf("\tType and State[%d]:", i);
if (pIPAddrTable->table[i].wType & MIB_IPADDR_PRIMARY)
printf("\tPrimary IP Address");
if (pIPAddrTable->table[i].wType & MIB_IPADDR_DYNAMIC)
printf("\tDynamic IP Address");
if (pIPAddrTable->table[i].wType & MIB_IPADDR_DISCONNECTED)
printf("\tAddress is on disconnected interface");
if (pIPAddrTable->table[i].wType & MIB_IPADDR_DELETED)
printf("\tAddress is being deleted");
if (pIPAddrTable->table[i].wType & MIB_IPADDR_TRANSIENT)
printf("\tTransient address");
printf("\n");
} if (pIPAddrTable) {
FREE(pIPAddrTable);
pIPAddrTable = NULL;
} exit(0);
}
另外,别获取IP地址,多网卡的时候你就乱了。自己做个配置文件存放你的IP地址。
http://blog.csdn.net/kakaxia6337/archive/2010/08/24/5835875.aspx里面只用到pIPAddrTable->table[0]的ip,您为什么选择循环查询?是因为可能有多个网卡吗?
我说下自己理解,请指正下。dwAddr表示本机地址,dwMask表示子网掩码,dwIndex不知道,dwBCastAddr表示广播地址,dwReasmSize不知道。
如果我可以确定只有一张网卡,是否就能用pIPAddrTable->table[0]来获取?
http://topic.csdn.net/u/20110212/14/5a7417e1-cb64-4bd5-b5c0-de2c5a7cfc2d.html