封装获取网络信息Linux—API类
2014-02-22 11:56
246 查看
[b]封装获取网络信息Linux—API类[/b]
封装好的库:
测试代码:
封装好的库:
#ifndef NETINFORMATION_H #define NETINFORMATION_H #include <netdb.h>//包含gethostbyname gethostbyaddr #include <netinet/in.h> class NetInformation { private: struct hostent *hostInformation; struct servent *hostServer; public: void reset(); void getHostInfoByAddr(); void getHostInfoByName(); void printHostInformation(); void getHostServer(const char *name,const char *proto); void getHostServer(int port,const char *proto); void printHostServer(); }; #endif
#include "NetInformation.h" #include <unistd.h>//包含 gethostname #include <netinet/in.h>//此文件中包含 in_addr #include <arpa/inet.h> //此文件中包含 inet_ntoa #include <iostream> #include <cstring> using std::cout; using std::endl; void NetInformation::getHostInfoByName() { char hostName[256]; if(gethostname(hostName,255)==0) ;//成功时返回0,失败时返回-1 else cout<<"gethostname failed"; hostInformation=gethostbyname(hostName); } void NetInformation::getHostInfoByAddr() { struct in_addr hostAddr; char addr[12]; strcpy(addr,"127.0.0.1"); inet_aton(addr,&hostAddr); hostInformation=gethostbyaddr(&hostAddr,sizeof(&hostAddr),AF_INET); } void NetInformation::printHostInformation() { char **ptr,**pptr,str[32]; cout<<"主机名:"<<hostInformation->h_name<<endl; cout<<"主机别名:"; ptr = hostInformation->h_aliases; if(*ptr==0) cout<<"没有查询到主机别名"; while(*ptr) { cout<<*ptr; ++ptr; }; cout<<endl; //根据地址类型,将地址打出来 switch(hostInformation->h_addrtype) { case AF_INET: case AF_INET6: pptr=hostInformation->h_addr_list; //将刚才得到的所有地址都打出来。其中调用了inet_ntop()函数 while(*pptr) { cout<<"主机地址:"<<inet_ntop(hostInformation->h_addrtype, *pptr, str, sizeof(str)); ++pptr; } cout<<endl; break; default: cout<<"unknown address type\n"; break; } } void NetInformation::getHostServer(const char *name,const char *proto) { hostServer=getservbyname(name,proto); } void NetInformation::getHostServer(int port,const char *proto) { hostServer=getservbyport(port,proto); } void NetInformation::printHostServer() { if(hostServer!=0) { cout<<"服务名 :"<<hostServer->s_name<<endl; char **alisases=hostServer->s_aliases; cout<<"服务别名:"; if(*alisases==0) cout<<"未查询到别名"<<endl; else { while(*alisases) { cout<<*alisases; ++alisases; } cout<<endl; } cout<<"端口号:"<<hostServer->s_port<<endl; cout<<"套接字类型:"<<hostServer->s_proto<<endl; } } void NetInformation::reset() { hostInformation=0; hostServer=0; }
测试代码:
#include <iostream> #include <unistd.h> #include <netinet/in.h> #include "NetInformation.h" using namespace std; int main() { NetInformation test; cout<<"/**< 方式一*/"<<endl; test.getHostInfoByName(); test.printHostInformation(); cout<<"/**< 方式二 */"<<endl; test.reset(); test.getHostInfoByAddr(); test.printHostInformation(); cout<<"/**< 方式三 */"<<endl; test.reset(); test.getHostServer("daytime","tcp"); test.printHostServer(); cout<<"/**< 方式四 */"<<endl; test.reset(); test.getHostServer(3328,"tcp"); test.printHostServer(); return 0; }
相关文章推荐
- 通过广播方式获取当前网络类型,如果是Wifi,获取对应的信息(附加有root之后可实现保存的无线网名称及密码获取)
- 用ioctl获取无线网络信息 /usr//include/linux/wireless.h
- linux 系统获取网络ip, mask, gateway, dns信息小程序
- Android 获取手机设备信息、网络信息、SIM卡信息
- Qt网络获取外网的网络信息
- Qt获取网络信息
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第二节 获取MIB-II 接口表
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第十七节 设置TCP连接状态
- Linux编程获取网络信息总结
- Python绝技笔记--------python操作注册表来获取之前连接的网络信息
- 使用WinPcap编程(1)——获取网络设备信息
- android之GPS或网络获取经纬度信息
- Linux下通过ioctl系统调用来获取和设置网络信息
- 在C#中调用API获取网络信息和流量
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第五节 获取本机UDP信息列表
- 在vb中使用Iphlpapi.dll获取网络信息 第二章 第二十节 测定到指定目的地往返时间和跳跃数
- Qt-获取主机网络信息之QNetworkAddressEntry
- ZMY_HTttpUtils获取网络信息
- QT获取本机网络信息
- 27-网络编程-22-网络编程(模拟一个浏览器获取信息)