封装获取网络信息Linux—API类

封装好的库:

 #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[]; if(gethostname(hostName,)==) ;//成功时返回0,失败时返回-1
else cout<<"gethostname failed";
hostInformation=gethostbyname(hostName);
} void NetInformation::getHostInfoByAddr()
{
struct in_addr hostAddr;
char addr[]; strcpy(addr,"127.0.0.1");
inet_aton(addr,&hostAddr);
hostInformation=gethostbyaddr(&hostAddr,sizeof(&hostAddr),AF_INET);
} void NetInformation::printHostInformation()
{
char **ptr,**pptr,str[];
cout<<"主机名:"<<hostInformation->h_name<<endl; cout<<"主机别名:";
ptr = hostInformation->h_aliases;
if(*ptr==)
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!=)
{
cout<<"服务名 :"<<hostServer->s_name<<endl; char **alisases=hostServer->s_aliases;
cout<<"服务别名:";
if(*alisases==) cout<<"未查询到别名"<<endl;
else
{
while(*alisases)
{
cout<<*alisases;
++alisases;
}
cout<<endl;
}
cout<<"端口号:"<<hostServer->s_port<<endl;
cout<<"套接字类型:"<<hostServer->s_proto<<endl;
}
} void NetInformation::reset()
{
hostInformation=;
hostServer=; }

测试代码:

#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(,"tcp");
test.printHostServer();
return ;
}

封装获取网络信息Linux—API类的更多相关文章

  1. 在C#中调用API获取网络信息和流量

    原文 在C#中调用API获取网络信息和流量 最近一项目中要求显示网络流量,而且必须使用C#. 事实上,调用 IpHlpApi.dll 的 GetIfTable API 可以轻易获得网络信息和网络流量. ...

  2. 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化

    [源码下载] 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...

  3. React Native之获取通讯录信息并实现类通讯录列表(ios android)

    React Native之获取通讯录信息并实现类通讯录列表(ios android) 一,需求分析 1,获取通讯录信息,筛选出通讯录里有多少好友在使用某个应用. 2,获取通讯录信息,实现类通讯录,可拨 ...

  4. appium自动化测试框架——封装获取设备信息类

    在上一节中,我们已经解决了如何在python中执行cmd,并获取执行结果.下面就小小实战一下,获取设备信息. 一.思路 1.windows上获取设备信息的方法 输入dos命令“adb devices” ...

  5. Android简易实战教程--第四十七话《使用OKhttp回调方式获取网络信息》

    在之前的小案例中写过一篇使用HttpUrlConnection获取网络数据的例子.在OKhttp盛行的时代,当然要学会怎么使用它,本篇就对其基本使用做一个介绍,然后再使用它的接口回调的方式获取相同的数 ...

  6. [整]C#获取天气预报信息(baidu api)包括pm2.5

    /// <summary> /// 获取天气预报信息 /// </summary> /// <returns></returns> public Bai ...

  7. httpClient实现获取网络信息

    自己实现的抓取网络信息 package util; import java.io.IOException; import java.lang.reflect.Field; import java.ma ...

  8. qt获取网络ip地址的类

    最近在学习qt网络编程,基于tcp和udp协议. 看了一些别人的程序和qt4自带的例子,困扰我最大的问题就是获取ip的类,总结起来还挺多的. 主要介绍常用的QtNetwork Module中的QHos ...

  9. 基于AFNetWorking封装一个网络请求数据的类

    1.新建一个继承于NSObject类的类,在.h文件中 #import "AFHTTPRequestOperationManager.h" //定义两个block来接收请求成功和失 ...

随机推荐

  1. 使用ASP在IIS创建WEB站点的函数

    程序代码: '=========================================================='函数介绍:创建WebSite'本函数使用ADSI,需要Adminis ...

  2. 无法绑定到新的显示成员,参数名:newDisplayMember

    此问题不是网上说的 DisplayMember 等先后顺序问题,即使更换绑定数序,只是把错误覆盖而已(绑定的是对象的类名) ValueMember = "Id"; DisplayM ...

  3. 最新的 iOS 申请证书与发布流程(2016.12)

    今天刚好客户定制的APP需要上架,也提供了新的开发者账号,所以就顺带把申请证书与发布流程写一遍. 证书是什么? 上面这个就是我们申请好证书后,下载到本地的.cer文件,也就是常说的开发证书与发布证书的 ...

  4. 【BZOJ1875】【矩阵乘法】[SDOI2009]HH去散步

    Description HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走,就是散步,就是在一定的时间 内,走过一定的距离. 但是同时HH又是个喜欢变化的人,所以他不会立刻沿着刚刚走来的路走回. 又因 ...

  5. 搭建VPN服务器之PPTP

    搭建VPN服务器之PPTP 1. 查看系统是否支持PPP 一般自己的系统支持,VPS需要验证. [root@oldboyedu ~]# cat /dev/ppp cat: /dev/ppp: No s ...

  6. nginx 基础文档

    Nginx基础 1.  nginx安装 2.  nginx 编译参数详解 3.  nginx安装配置+清缓存模块安装 4.  nginx+PHP 5.5 5.  nginx配置虚拟主机 6.  ngi ...

  7. python包管理器pip

    步骤一:下载pip包 https://pypi.python.org/pypi/pip 步骤二:安装pip包 解压后,到pip包目录执行: python setup.py install 步骤三:添加 ...

  8. Javascript AMD模块化规范-备用

    AMD是"Asynchronous Module Definition"的缩写,意思是"异步模块定义". 模块定义define(id?, dependencie ...

  9. jQuery遍历对象、数组、集合实例

    1.jquery 遍历对象 复制代码代码如下:   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ...

  10. HNOI2015滚粗记

    HNOI2015滚粗记 经过两天的苦战,艰难的HNOI终于结束了.感觉这次HNOI自己还是收获了许多. \(Day1\)打的很是艰难,题目一下就有种晕头转向的感觉.开场\(20min\)自己还在读题时 ...