用的是gethostbyname方法,获取到的IP有好几个,有我当前本地连接的IP地址,还有虚拟机的两个IP地址,甚至还有我以前删除过的本地连接的IP地址...
请问我如何能区分呢??
我只想要我当前使用的IP地址,谢谢帮忙!!!
请问我如何能区分呢??
我只想要我当前使用的IP地址,谢谢帮忙!!!
解决方案 »
- VFW用无驱动的USB摄像头的问题
- 请教关于OnMove的问题
- 求救,怎样在mfc中定位到word的书签,请给出具体代码,在线等
- 怎样在数据库读出的字符与ComboBox中某一项对应显示
- windows是如何将外设动作翻译为WM_***消息的?
- VC中出现的一个错误提示,帮忙看看
- MFC怎样定义一个结构体,用来保存文件夹绝对路径和可以访问它的用户
- 阿拉伯数字转换中文数字 有什么好的方法 越简单越好!
- ADO中查找结果的放在recordset对象中,请问这种对象在内存中的存放结构是什么样的?以什么标志结束?
- 有大哥详细解释消息映射的机制及AppWizard给我们添加的代码吗?
- 端口监听?
- sendmessage或postmessage向指定窗口发送鼠标消息的问题 在线等
// IPv4 addresses, gateways, and secondary WINS servers
// assigned to the adapter.
// Note that this sample code only prints out the
// first entry for the IP address/mask, gateway,
// and secondary WINS server for each adapter. PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;/* variables used to print DHCP time info */
struct tm newtime;
char buffer[32];
errno_t error;pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);// Make an initial call to GetAdaptersInfo to get
// the necessary size into the ulOutBufLen variable
if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
if (pAdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
return 1;
}if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
pAdapter = pAdapterInfo;
while (pAdapter) {
printf("\tAdapter Name: \t%s\n", pAdapter->AdapterName);
printf("\tAdapter Desc: \t%s\n", pAdapter->Description);
printf("\tAdapter Addr: \t");
for (UINT i = 0; i < pAdapter->AddressLength; i++) {
if (i == (pAdapter->AddressLength - 1))
printf("%.2X\n",(int)pAdapter->Address[i]);
else
printf("%.2X-",(int)pAdapter->Address[i]);
}
printf("\tIP Address: \t%s\n", pAdapter->IpAddressList.IpAddress.String);
printf("\tIP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String); printf("\tGateway: \t%s\n", pAdapter->GatewayList.IpAddress.String);
printf("\t***\n"); if (pAdapter->DhcpEnabled) {
printf("\tDHCP Enabled: Yes\n");
printf("\t\tDHCP Server: \t%s\n", pAdapter->DhcpServer.IpAddress.String); printf("\tLease Obtained: ");
/* Display local time */
error = _localtime32_s(&newtime, &pAdapter->LeaseObtained);
if (error)
printf("\tInvalid Argument to _localtime32_s\n");
else {
// Convert to an ASCII representation
error = asctime_s(buffer, 32, &newtime);
if (error)
printf("Invalid Argument to asctime_s\n");
else
/* asctime_s returns the string terminated by \n\0 */
printf("%s", buffer);
} printf("\tLease Expires: ");
error = _localtime32_s(&newtime, &pAdapter->LeaseExpires);
if (error)
printf("Invalid Argument to _localtime32_s\n");
else {
// Convert to an ASCII representation
error = asctime_s(buffer, 32, &newtime);
if (error)
printf("Invalid Argument to asctime_s\n");
else
/* asctime_s returns the string terminated by \n\0 */
printf("%s", buffer);
}
}
else
printf("\tDHCP Enabled: No\n");
if (pAdapter->HaveWins) {
printf("\tHave Wins: Yes\n");
printf("\t\tPrimary Wins Server: \t%s\n", pAdapter->PrimaryWinsServer.IpAddress.String);
printf("\t\tSecondary Wins Server: \t%s\n", pAdapter->SecondaryWinsServer.IpAddress.String);
}
else
printf("\tHave Wins: No\n");
pAdapter = pAdapter->Next;
}
}
else {
printf("GetAdaptersInfo failed with error: %d\n", dwRetVal);if (pAdapterInfo)
free(pAdapterInfo);
}
如果你机器接了多块网卡,还都能连上网,没啥好办法区分一下,就都找出来,一个一个对应一下吧,要用哪个,选择一下!
我需要的就是用代码挑出我要的IP地址,不可能让人去自己选择要哪个的。