封装获取网络信息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. SQL学习:主键,外键,主键表,外键表,数据库的表与表之间的关系;

    在数据库的学习中,对于一个表的主键和外键的认识是非常重要的. 主键:在一个表中,能唯一的表示一个事物(或者一条记录)的字段,我们称之为主键 注意: 主键的设置可以不只是用一个字段,也可以用若干个字段的 ...

  2. linux rman shell

    # make direcory for backset file and scripts file,in my case /backup/db_bak cd   /backup/db_bak mkdi ...

  3. vi 替换字符串

    假如说我想把该文件中所有的Web替换成SOR_SYS,那么我们可以用vi打开该文件,然后按一下: 你的命令行的最后一行会出现:,这个时候就是提醒你输入替换的命令 %s/Web/SOR_SYS/g 按一 ...

  4. 使用正则表达式统计vs项目代码总行数[转]

    怎么统计VS2008中工程的总共代码行数?怎么统计VS2008中工程的总共代码行数?在一个大工程中有很多的源文件和头文件,我如何快速统计总行数? ------解决方案----------------- ...

  5. Js浏览器对象

    Js浏览器对象——window对象 1.window对象: (1)window对象是BOM的核心,window对象指当前的浏览器窗口. (2)所有的JavaScript全局对象.函数以及变量均自动成为 ...

  6. Eclipse不能自动编译 java文件,不会生成CLASS

    每次修改类代码后都得重启 Tomcat 花了1天终于解决,网上所说基本是下面1和2的方法,使用之后还是不行最后重新建工作环境导入项目对比了一下找到第三种方法 1.Project 下有个 "B ...

  7. Java面向对象程序设计--接口和内部类

    1.接口的定义: In the Java programming language, an interface is not a class but          staff[0] =       ...

  8. mount的艺术

    在阅读本文之前,我假设你已经对Linux系统下的硬盘.光盘的设备命令规则有所了解,比如sda和sda1的关系,以及hda.sda.fd.cdrom等设备. === 1 我把U盘插到USB口上了,下一步 ...

  9. PHP实战开发教程

    对于PHP初学者来说,一上手就学习庞大的PHP语法无疑很打击自信心.其实即便是很熟练的程序员,也未必对所有的语法非常熟悉.通常熟练的程序员比普通的程序员的优势在于对基本语法的理解非常透彻,而且常用的一 ...

  10. Centos7 修改运行级别

    systemd使用比sysvinit的运行级别更为自由的target概念作为替代 第三运行级: multi-user.target 第五运行级: graphical.target   #前者是符号链接 ...