您的位置:首页 > 理论基础 > 计算机网络

封装获取网络信息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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: