封装获取网络信息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. C#中的委托范例学习

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  2. [Twisted] deferred

    Twisted提供一个优雅的实现(Deferred)来管理回调函数. Deferred Object 的结构 Deferred Object包含两个回调函数列表.一个用来保存成功的回调函数,另一个用来 ...

  3. AngularJS cookie的使用

    Javascript使用document.cookie接口来处理简单的文本cookie,但现在大多数现代浏览器都可以使用html5 API了(sessionstorage和localstorage), ...

  4. MySQL常用函数 转载

    一.数学函数ABS(x)                    返回x的绝对值BIN(x) 返回x的二进制(OCT返回八进制,HEX返回十六进制)CEILING(x)                返 ...

  5. 《wc》-linux命令五分钟系列之十七

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...

  6. centos7 玩aapt 安卓应用apk解包工具的安装

    最近在做一个应用市场的项目,需要在centos7下面对apk解包读取其信息,这就想到了使用Google的解包工具aapt,但是由于中国的原因,国内访问原生工具的地址就有些麻烦,这里就贴出地址:http ...

  7. c#配置文件appStrings配置节的读取、添加和修改

    程序开发中经常会用到应用程序配置文件,好处就是维护人员可以直接修改配置文件进行维护,而不用修改程序.好,切入主题. 给项目添加应用程序配置文件App.config,先在里面写几句: <?xml ...

  8. oracle删除用户所有表

    在删除数据表的时候往往遇到外键约束无法删除的情况,我们可以通过以下几步将数据库表删除,建议在删除库之前先对数据库进行备份,养成良好习惯. 1.删除外键 --查询用户所有表的外键,owner条件为use ...

  9. Android内的生命周期整理

    1. Android App的生命周期: 2. Application的生命周期: 3. Activity的生命周期: 3.1 Fragment的生命周期: 4. Service的生命周期:5. Br ...

  10. Python函数式编程初级学习

    函数式编程即函数可以作为参数传入函数,也可以返回函数. 1.高阶函数     函数可以作为参数传入函数.     def add(x,y,f):         return f(x)+f(y)   ...