C++如何用函数获得本机打IP地址?
展开全部
c++用函数获取本机的IP地址方法如下:
#include <winsock2.h>
// Add 'ws2_32.lib' to your linker options
WSADATA WSAData;
// Initialize winsock dll
if(::WSAStartup(MAKEWORD(1, 0), &WSAData))
{
// Error handling
}
// Get local host name
char szHostName[128] = "";
if(::gethostname(szHostName, sizeof(szHostName)))
{
// Error handling -> call 'WSAGetLastError()'
}
// Get local IP addresses
struct sockaddr_in SocketAddress;
struct hostent *pHost = 0;
pHost = ::gethostbyname(szHostName);
if(!pHost)
{
// Error handling -> call 'WSAGetLastError()'
}
char aszIPAddresses[10][16]; // maximum of ten IP addresses
for(int iCnt = 0; ((pHost->h_addr_list[iCnt]) && (iCnt < 10)); ++iCnt)
{
memcpy(&SocketAddress.sin_addr, pHost->h_addr_list[iCnt], pHost->h_length);
strcpy(aszIPAddresses[iCnt], inet_ntoa(SocketAddress.sin_addr));
}
// Cleanup
WSACleanup();
我以前也是从网上了解到的,希望可以给你带来帮助
#include <winsock2.h>
// Add 'ws2_32.lib' to your linker options
WSADATA WSAData;
// Initialize winsock dll
if(::WSAStartup(MAKEWORD(1, 0), &WSAData))
{
// Error handling
}
// Get local host name
char szHostName[128] = "";
if(::gethostname(szHostName, sizeof(szHostName)))
{
// Error handling -> call 'WSAGetLastError()'
}
// Get local IP addresses
struct sockaddr_in SocketAddress;
struct hostent *pHost = 0;
pHost = ::gethostbyname(szHostName);
if(!pHost)
{
// Error handling -> call 'WSAGetLastError()'
}
char aszIPAddresses[10][16]; // maximum of ten IP addresses
for(int iCnt = 0; ((pHost->h_addr_list[iCnt]) && (iCnt < 10)); ++iCnt)
{
memcpy(&SocketAddress.sin_addr, pHost->h_addr_list[iCnt], pHost->h_length);
strcpy(aszIPAddresses[iCnt], inet_ntoa(SocketAddress.sin_addr));
}
// Cleanup
WSACleanup();
我以前也是从网上了解到的,希望可以给你带来帮助
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家提供动态代理服务器服务的企业,旨在帮助用户更好地管理网络访问和安全。以下是一些关于StormProxies的IP动态代理服务的特点:1. 高匿名性:StormProxies的动态代理服务器具有高匿名性,可以有效...
点击进入详情页
本回答由Storm代理提供
展开全部
//c++获取本机IP
#include <stdio.h>
#include <stdlib.h>
#include <WINSOCK2.H>
#pragma comment(lib,"ws2_32.lib")
int getIP(int ,char**)
{
char host_name[255];
if(gethostname(host_name,sizeof(host_name))==SOCKET_ERROR)
{
printf("Error %d when getting local host name\n",WSAGetLastError());
return -1;
}
printf("host name:%s\n",host_name);
struct hostent *phe=gethostbyname(host_name);
if(phe==0)
{
printf("Error host lookup\n");
return -1;
}
for(int i=0;phe->h_addr_list[i]!=0;++i)
{
struct in_addr addr;
memcpy(&addr,phe->h_addr_list[i],sizeof(struct in_addr));
printf("Address %d :%s\n",i,inet_ntoa(addr));
}
return 0;
}
int main(int argc,char *argv[])
{
WSAData wsaData;
if(WSAStartup(MAKEWORD(1,1),&wsaData)!=0)
{
system("pause");
return -1;
}
int result=getIP(argc,argv);
WSACleanup();
system("pause");
return result;
}
#include <stdio.h>
#include <stdlib.h>
#include <WINSOCK2.H>
#pragma comment(lib,"ws2_32.lib")
int getIP(int ,char**)
{
char host_name[255];
if(gethostname(host_name,sizeof(host_name))==SOCKET_ERROR)
{
printf("Error %d when getting local host name\n",WSAGetLastError());
return -1;
}
printf("host name:%s\n",host_name);
struct hostent *phe=gethostbyname(host_name);
if(phe==0)
{
printf("Error host lookup\n");
return -1;
}
for(int i=0;phe->h_addr_list[i]!=0;++i)
{
struct in_addr addr;
memcpy(&addr,phe->h_addr_list[i],sizeof(struct in_addr));
printf("Address %d :%s\n",i,inet_ntoa(addr));
}
return 0;
}
int main(int argc,char *argv[])
{
WSAData wsaData;
if(WSAStartup(MAKEWORD(1,1),&wsaData)!=0)
{
system("pause");
return -1;
}
int result=getIP(argc,argv);
WSACleanup();
system("pause");
return result;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-06-25
展开全部
#include <winsock2.h>// Add 'ws2_32.lib' to your linker optionsWSADATA WSAData;// Initialize winsock dll
if(::WSAStartup(MAKEWORD(1, 0), &WSAData))
{
// Error handling
}// Get local host name
char szHostName[128] = "";if(::gethostname(szHostName, sizeof(szHostName)))
{
// Error handling -> call 'WSAGetLastError()'
}// Get local IP addresses
struct sockaddr_in SocketAddress;
struct hostent *pHost = 0;pHost = ::gethostbyname(szHostName);
if(!pHost)
{
// Error handling -> call 'WSAGetLastError()'
}char aszIPAddresses[10][16]; // maximum of ten IP addressesfor(int iCnt = 0; ((pHost->h_addr_list[iCnt]) && (iCnt < 10)); ++iCnt)
{
memcpy(&SocketAddress.sin_addr, pHost->h_addr_list[iCnt], pHost->h_length);
strcpy(aszIPAddresses[iCnt], inet_ntoa(SocketAddress.sin_addr));
}// Cleanup
WSACleanup(); 摘自:http://www.codeguru.com/forum/showthread.php?t=233261获取最多10个本机ip
if(::WSAStartup(MAKEWORD(1, 0), &WSAData))
{
// Error handling
}// Get local host name
char szHostName[128] = "";if(::gethostname(szHostName, sizeof(szHostName)))
{
// Error handling -> call 'WSAGetLastError()'
}// Get local IP addresses
struct sockaddr_in SocketAddress;
struct hostent *pHost = 0;pHost = ::gethostbyname(szHostName);
if(!pHost)
{
// Error handling -> call 'WSAGetLastError()'
}char aszIPAddresses[10][16]; // maximum of ten IP addressesfor(int iCnt = 0; ((pHost->h_addr_list[iCnt]) && (iCnt < 10)); ++iCnt)
{
memcpy(&SocketAddress.sin_addr, pHost->h_addr_list[iCnt], pHost->h_length);
strcpy(aszIPAddresses[iCnt], inet_ntoa(SocketAddress.sin_addr));
}// Cleanup
WSACleanup(); 摘自:http://www.codeguru.com/forum/showthread.php?t=233261获取最多10个本机ip
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询