国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

封裝獲取網絡信息Linux—API類

2024-06-28 13:27:56
字體:
來源:轉載
供稿:網友
封裝獲取網絡信息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;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 扎赉特旗| 南华县| 襄汾县| 白银市| 无锡市| 开原市| 喀喇沁旗| 彭泽县| 土默特右旗| 荣昌县| 上饶县| 金阳县| 台东市| 桦川县| 开鲁县| 烟台市| 忻州市| 台江县| 建始县| 河南省| 文成县| 疏勒县| 日喀则市| 封丘县| 循化| 拜城县| 樟树市| 惠州市| 凯里市| 郧西县| 陆河县| 西平县| 抚州市| 花莲县| 闽清县| 阜康市| 呼图壁县| 乐清市| 开平市| 黄平县| 漯河市|