-
2013-04-08 13:22:24
/* 编译环境: visual c++ */ #include <stdio.h> #include <winsock2.h> #pragma comment(lib,"ws2_32.lib") int doit(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 is: %s\n", host_name); //从主机名数据库中得到对应的“主机” struct hostent *phe = gethostbyname(host_name); if (phe == 0) { printf("Yow! Bad host lookup."); return 1; } //循环得出本地机器所有IP地址 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) { return 255; } int retval = doit(argc, argv); WSACleanup(); return retval; }
更多相关内容 -
C++获取当前连接IP和主机名.rar
2016-05-30 16:00:22C++获取当前连接IP和主机名.rar -
C++ | 获取主机名和IP地址
2019-04-26 13:38:50C++ | 获取主机名和IP地址 项目需求搞的一个函数,方便获取无线IP地址。 1 函数 /** * 获取机器Ip地址和主机名 */ bool getHostNameAndIp(std::string& strWLANIp, std::string& strLocalIp) { PIP_...C++ | 获取主机名和IP地址
项目需求搞的一个函数,方便获取无线IP地址。
1 函数
/** * 获取机器Ip地址和主机名 */ bool getHostNameAndIp(std::string& strWLANIp, std::string& strLocalIp) { PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO(); //得到结构体大小,用于GetAdaptersInfo参数 unsigned long stSize = sizeof(IP_ADAPTER_INFO); //调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量;其中stSize参数既是一个输入量也是一个输出量 int nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize); //记录网卡数量 int netCardNum = 0; //记录每张网卡上的IP地址数量 int IPnumPerNetCard = 0; if (ERROR_BUFFER_OVERFLOW == nRel) { //如果函数返回的是ERROR_BUFFER_OVERFLOW //则说明GetAdaptersInfo参数传递的内存空间不够,同时其传出stSize,表示需要的空间大小 //这也是说明为什么stSize既是一个输入量也是一个输出量 //释放原来的内存空间 delete pIpAdapterInfo; //重新申请内存空间用来存储所有网卡信息 pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[stSize]; //再次调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量 nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize); } std::string strMAC; strMAC.resize(20); if (ERROR_SUCCESS == nRel) { //输出网卡信息 //可能有多网卡,因此通过循环去判断 while (pIpAdapterInfo) { //可能网卡有多IP,因此通过循环去判断 IP_ADDR_STRING *pIpAddrString = &(pIpAdapterInfo->IpAddressList); if (pIpAddrString) { sprintf_s(const_cast<char*>(strMAC.c_str()), 20, "%02X-%02X-%02X-%02X-%02X-%02X", pIpAdapterInfo->Address[0], pIpAdapterInfo->Address[1], pIpAdapterInfo->Address[2], pIpAdapterInfo->Address[3], pIpAdapterInfo->Address[4], pIpAdapterInfo->Address[5]); //包含以下MAC地址的前8个字节(前3段)是虚拟网卡 //"00:05:69"; //vmware1 //"00:0C:29"; //vmware2 //"00:50:56"; //vmware3 //"00:1c:14"; //vmware4 //"00:1C:42"; //parallels1 //"00:03:FF"; //microsoft virtual pc //"00:0F:4B"; //virtual iron 4 //"00:16:3E"; //red hat xen , oracle vm , xen source, novell xen //"08:00:27"; //virtualbox // 说明是本地网络 if (pIpAdapterInfo->Type == MIB_IF_TYPE_ETHERNET) { if (!strstr(pIpAdapterInfo->Description, "Virtual")) { std::string strIp = pIpAddrString->IpAddress.String; if (strIp.compare("0.0.0.0") != 0) { strLocalIp = strIp; } } } // 说明是无线网络 if (pIpAdapterInfo->Type == 71) { std::string strIp = pIpAddrString->IpAddress.String; if (strIp.compare("0.0.0.0") != 0) { strWLANIp = strIp; } } } pIpAdapterInfo = pIpAdapterInfo->Next; } } return true; }
2 使用方法
std::string ip, host; if (!getHostNameAndIp(ip, host)) return false; // ip即得到IP地址,host主机地址 std::cout<< ip<< " "<< host<< endl;
-
7.如何获取主机名和IP地址?(Visual C++编程 源代码)
2022-06-26 17:03:297.如何获取主机名和IP地址?(Visual C++编程 源代码)7.如何获取主机名和IP地址?(Visual C++编程 源代码)7.如何获取主机名和IP地址?(Visual C++编程 源代码)7.如何获取主机名和IP地址?(Visual C++编程 源... -
获取本地主机名和IP地址的C++ Windows函数调用
2021-07-26 04:44:01您可以获取ip地址,端口,sockaddr_in,端口。BOOL GetMyHostName(LPSTR pszBuffer, UINT nLen){BOOL ret;ret = FALSE;if (pszBuffer && nLen){if (gethostname(pszBuffer, nLen) == ...这是一个多平台解决方案... Windows,Linux和MacOSX。 您可以获取ip地址,端口,sockaddr_in,端口。
BOOL GetMyHostName(LPSTR pszBuffer, UINT nLen)
{
BOOL ret;
ret = FALSE;
if (pszBuffer && nLen)
{
if (gethostname(pszBuffer, nLen) == 0)
ret = TRUE;
else
*pszBuffer = '\0';
}
return ret;
}
ULONG GetPeerName(SOCKET _clientSock, LPSTR _pIPStr, UINT _IPMaxLen, int *_pport)
{
struct sockaddr_in sin;
unsigned long ipaddr;
ipaddr = INADDR_NONE;
if (_pIPStr && _IPMaxLen)
*_pIPStr = '\0';
if (_clientSock!=INVALID_SOCKET)
{
#if defined(_WIN32)
int locallen;
#else
UINT locallen;
#endif
locallen = sizeof(struct sockaddr_in);
memset(&sin, '\0', locallen);
if (getpeername(_clientSock, (struct sockaddr *) &sin, &locallen) == 0)
{
ipaddr = GetSinIP(&sin, _pIPStr, _IPMaxLen);
if (_pport)
*_pport = GetSinPort(&sin);
}
}
return ipaddr;
}
ULONG GetSinIP(struct sockaddr_in *_psin, LPSTR pIPStr, UINT IPMaxLen)
{
unsigned long ipaddr;
ipaddr = INADDR_NONE;
if (pIPStr && IPMaxLen)
*pIPStr = '\0';
if (_psin)
{
#if defined(_WIN32)
ipaddr = _psin->sin_addr.S_un.S_addr;
#else
ipaddr = _psin->sin_addr.s_addr;
#endif
if (pIPStr && IPMaxLen)
{
char *pIP;
struct in_addr in;
#if defined(_WIN32)
in.S_un.S_addr = ipaddr;
#else
in.s_addr = ipaddr;
#endif
pIP = inet_ntoa(in);
if (pIP)
adjust_str(pIP, pIPStr, IPMaxLen);
}
}
return ipaddr;
}
int GetSinPort(struct sockaddr_in *_psin)
{
int port;
port = 0;
if (_psin)
port = _Xntohs(_psin->sin_port);
return port;
}
-
C++获取本地计算机主机名和IP
2015-07-06 12:52:57//获取主机名:也可以使用GetComputerName()这个函数 if(gethostname(host,sizeof(host))==SOCKET_ERROR) { cout无法获取主机名..."; } else { cout本机计算机名为:"; } //获取计算机IP:...#include <iostream>
using namespace std;
#include "winsock2.h"
#pragma comment(lib,"ws2_32.lib")
void main()
{
//初始化:如果不初始化,以下代码将无法执行
WSAData data;
if(WSAStartup(MAKEWORD(1,1),&data)!=0)
{
cout<<"初始化错误,无法获取主机信息..."<<endl ;
}
char host[255];
//获取主机名:也可以使用GetComputerName()这个函数
if(gethostname(host,sizeof(host))==SOCKET_ERROR)
{
cout<<"无法获取主机名..."<<endl;
}
else
{
cout<<"本机计算机名为:"<<host<<endl;
}//获取计算机IP:gethostbyname也需要初始化(上面已初始化)
struct hostent *p=gethostbyname(host);
if(p==0)
{
cout<<"无法获取计算机主机名及IP..."<<endl;
}
else
{
//获取本机计算机名
//cout<<"本机计算机名为:"<<p->h_name<<endl;//本机IP:利用循环,输出本机所有IP
for(int i=0;p->h_addr_list[i]!=0;i++)
{
struct in_addr in;
memcpy(&in,p->h_addr_list[i],sizeof(struct in_addr));
cout<<"第"<<i+1<<"块网卡的IP为:"<<inet_ntoa(in)<<endl;
}}
WSACleanup();
cin.get();
} -
C++ 实现获取本机IP与MAC地址详细信息
2015-04-19 20:08:24一个用C++控制台实现的,获取本机IP与MAC地址的小程序,用于初学者参考 -
Linux之C++获取系统用户名
2022-04-01 15:19:03在 linux 系统上开发时,有时需要判断当前用户名,来做针对性的功能设计,比如不同用户写的日志文件路径不同,因此,本文对获取用户名方法做一个介绍 1 数据结构说明 在 passwd 结构体中含有相关的定义 struct ... -
获取计算机名和IP地址(linux c++版本)
2021-05-13 00:37:53/*获取计算机名和IP地址(linux c++版本)root@yiyouserver:~/XWH/xwh# g++ -o gethostname gethostname.cpproot@yiyouserver:~/XWH/xwh# ./gethostname计算机名:yiyouserverIP:192.168.205.128*/#include #include //... -
C++获取系统信息(IP地址、硬件信息等)
2022-01-27 17:28:08if(gethostname(hostName,sizeof(hostName))) //获取主机名 { printf("Error: %u\n", WSAGetLastError()); exit(-1); //异常退出 } printf("主机名: %s\n", hostName); hostent *host=gethostbyname(hostName); //... -
获取主机名和IP地址
2012-05-08 19:34:31用MFC实现,点击相应控件,然后文本框内显示主机名和IP地址。 -
VC++获取本机主机名和IP地址
2009-10-19 22:37:15VC++获取本机主机名和IP地址的源代码,此代码可以供各位学网络工程的同学研究研究~~ -
C++获取计算机名和 IP
2020-03-22 11:23:50转自:C++获取本地计算机主机名和IP #include <iostream> #include "winsock2.h" using namespace std; #pragma comment(lib,"ws2_32.lib") void main() { // 初始化:如果不初始化,以下代码将无法执行 ... -
linux下快速列出局域网中所有主机名(计算机名)的脚本
2021-01-09 02:19:37最近有列出局域网中所有主机名的需求(SMB协议里的),但是findsmb命令总是列不全,搜了搜网上也没什么现成的解决方案,于是自己写了个python脚本 脚本会扫描局域网arp表中所有ip,并尝试解析其主机名,这样可以较为... -
Linux C++获取系统名称和ip
2017-12-26 11:40:22使用封装的这个函数获取系统的名称和ip。 #include /* cout */ #include /* gethostname */ #include /* struct hostent */ #include /* inet_ntop */ bool GetHostInfo(std::string& hostName, std::string& Ip) ... -
C++ 获取主机IP DNS
2013-02-22 11:21:21获取主机名ip子网掩码网关MAC DHCP DNS -
C++ Scoket实现Client获取本机计算机名和ip地址发送到Server
2018-04-05 14:32:07包含Server.cpp和Client.cpp,简单的使用套接字实现Client获取本机计算机名和ip地址发送到Server -
C++ 获取本机的Ip地址及主机名
2012-03-07 11:02:17下面是获取本机Ip和主机名的程序,在VC6下成功,注意连接静态库的方法,初始化SOCKET库函数的方法, 以及取得gethostname的方法。 代码如下: #include #include #include using namespace std; #pragma ... -
C++获取URL中主机域名
2019-09-25 17:55:25std::string SplitHostDomain(const std::string& str) { if (!!str.compare(0,5,"http:")) { size_t found = str.find_first_of("/\\"); std::string str1 = str.substr(found + ... -
获取主机名和IP
2008-05-29 13:01:43<br> 一个VC++的MFC下的小程序, 自动获取本机的IP地址和主机名 -
windows下c++如何读取主机名
2017-09-03 10:46:38#include #include #include #include #include using namespace std;int main() { TCHAR buf[MAX_COMPUTERNAME_LENGTH + 2]; DWORD buf_size; buf -
获取主机名与外网ip
2012-12-03 11:13:51获取主机名 外网ip -
C++获得主机名和IP
2014-11-13 11:36:05char name[128]; hostent* pHost; gethostname(name, 128);//获得主机名 pHost = gethostbyname(name);//获得主机结构 本地IP = inet_ntoa(*((in_addr *)pHost->h_addr) -
C++ 获取局域网内所有可用IP和主机名
2014-12-10 14:35:24void GetNameAndIp() { struct hostent *host; struct in_addr *ptr; DWORD dwScope = RESOURCE_CONTEXT; NETRESOURCE *NetResource = NULL; HANDLE hEnum;... WNetOpenEnum(dwScope, NULL, NULL -
通过IP获取主机的名字
2013-08-15 14:52:39Unix/Linux下c语言,如何通过IP获取主机名的例子 -
C++ /MFC 获取硬盘序列号,获取主机名字
2019-11-02 08:45:14获取硬盘序列号 GetVolumeInformation("C:",NULL,MAX_PATH,&Serial,&Length,NULL,NULL,MAX_PATH); 文件服务器 ...在资源管理器的时候是\\IP\ShareName,所以...获得主机名 char szhostname[128]; if (... -
C++ | Qt 获取局域网中存在的主机(IP以及主机名)
2019-04-15 19:05:43这里主要是通过QHostInfo::lookupHost获取主机名,当然也可以通过IP找主机名,只要遍历局域网IP看其是否有主机名,就可以知道,这个IP是不是被使用(但是,某些开防火墙的机子,使用了IP,也是不能找到的) ...