封装获取网络信息Linux—API类
封装获取网络信息Linux—API类
封装好的库:
1 #ifndef NETINFORMATION_H 2 #define NETINFORMATION_H 3 4 #include <netdb.h>//包含gethostbyname gethostbyaddr 5 #include <netinet/in.h> 6 class NetInformation 7 { 8 private: 9 10 struct hostent *hostInformation; 11 struct servent *hostServer; 12 13 public: 14 void reset(); 15 16 void getHostInfoByAddr(); 17 void getHostInfoByName(); 18 void printHostInformation(); 19 20 void getHostServer(const char *name,const char *proto); 21 void getHostServer(int port,const char *proto); 22 void printHostServer(); 23 24 25 }; 26 27 #endif
1 #include "NetInformation.h" 2 3 4 #include <unistd.h>//包含 gethostname 5 #include <netinet/in.h>//此文件中包含 in_addr 6 #include <arpa/inet.h> //此文件中包含 inet_ntoa 7 #include <iostream> 8 #include <cstring> 9 using std::cout; 10 using std::endl; 11 12 13 void NetInformation::getHostInfoByName() 14 { 15 char hostName[256]; 16 17 if(gethostname(hostName,255)==0) ;//成功时返回0,失败时返回-1 18 else cout<<"gethostname failed"; 19 hostInformation=gethostbyname(hostName); 20 } 21 22 void NetInformation::getHostInfoByAddr() 23 { 24 struct in_addr hostAddr; 25 char addr[12]; 26 27 strcpy(addr,"127.0.0.1"); 28 inet_aton(addr,&hostAddr); 29 hostInformation=gethostbyaddr(&hostAddr,sizeof(&hostAddr),AF_INET); 30 } 31 32 void NetInformation::printHostInformation() 33 { 34 char **ptr,**pptr,str[32]; 35 cout<<"主机名:"<<hostInformation->h_name<<endl; 36 37 cout<<"主机别名:"; 38 ptr = hostInformation->h_aliases; 39 if(*ptr==0) 40 cout<<"没有查询到主机别名"; 41 while(*ptr) 42 { 43 cout<<*ptr; 44 ++ptr; 45 }; 46 cout<<endl; 47 48 //根据地址类型,将地址打出来 49 switch(hostInformation->h_addrtype) 50 { 51 case AF_INET: 52 case AF_INET6: 53 pptr=hostInformation->h_addr_list; 54 //将刚才得到的所有地址都打出来。其中调用了inet_ntop()函数 55 while(*pptr) 56 { 57 cout<<"主机地址:"<<inet_ntop(hostInformation->h_addrtype, *pptr, str, sizeof(str)); 58 ++pptr; 59 } 60 cout<<endl; 61 break; 62 default: 63 cout<<"unknown address type\n"; 64 break; 65 } 66 } 67 68 void NetInformation::getHostServer(const char *name,const char *proto) 69 { 70 hostServer=getservbyname(name,proto); 71 } 72 73 void NetInformation::getHostServer(int port,const char *proto) 74 { 75 hostServer=getservbyport(port,proto); 76 } 77 78 void NetInformation::printHostServer() 79 { 80 if(hostServer!=0) 81 { 82 cout<<"服务名 :"<<hostServer->s_name<<endl; 83 84 char **alisases=hostServer->s_aliases; 85 cout<<"服务别名:"; 86 if(*alisases==0) cout<<"未查询到别名"<<endl; 87 else 88 { 89 while(*alisases) 90 { 91 cout<<*alisases; 92 ++alisases; 93 } 94 cout<<endl; 95 } 96 cout<<"端口号:"<<hostServer->s_port<<endl; 97 cout<<"套接字类型:"<<hostServer->s_proto<<endl; 98 } 99 } 100 101 void NetInformation::reset() 102 { 103 hostInformation=0; 104 hostServer=0; 105 106 }
测试代码:
#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; }
不要让今天成为明天的遗憾!