封装获取网络信息Linux—API类
封装获取网络信息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类的更多相关文章
- 在C#中调用API获取网络信息和流量
原文 在C#中调用API获取网络信息和流量 最近一项目中要求显示网络流量,而且必须使用C#. 事实上,调用 IpHlpApi.dll 的 GetIfTable API 可以轻易获得网络信息和网络流量. ...
- 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化
[源码下载] 重新想象 Windows 8 Store Apps (60) - 通信: 获取网络信息, 序列化和反序列化 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...
- React Native之获取通讯录信息并实现类通讯录列表(ios android)
React Native之获取通讯录信息并实现类通讯录列表(ios android) 一,需求分析 1,获取通讯录信息,筛选出通讯录里有多少好友在使用某个应用. 2,获取通讯录信息,实现类通讯录,可拨 ...
- appium自动化测试框架——封装获取设备信息类
在上一节中,我们已经解决了如何在python中执行cmd,并获取执行结果.下面就小小实战一下,获取设备信息. 一.思路 1.windows上获取设备信息的方法 输入dos命令“adb devices” ...
- Android简易实战教程--第四十七话《使用OKhttp回调方式获取网络信息》
在之前的小案例中写过一篇使用HttpUrlConnection获取网络数据的例子.在OKhttp盛行的时代,当然要学会怎么使用它,本篇就对其基本使用做一个介绍,然后再使用它的接口回调的方式获取相同的数 ...
- [整]C#获取天气预报信息(baidu api)包括pm2.5
/// <summary> /// 获取天气预报信息 /// </summary> /// <returns></returns> public Bai ...
- httpClient实现获取网络信息
自己实现的抓取网络信息 package util; import java.io.IOException; import java.lang.reflect.Field; import java.ma ...
- qt获取网络ip地址的类
最近在学习qt网络编程,基于tcp和udp协议. 看了一些别人的程序和qt4自带的例子,困扰我最大的问题就是获取ip的类,总结起来还挺多的. 主要介绍常用的QtNetwork Module中的QHos ...
- 基于AFNetWorking封装一个网络请求数据的类
1.新建一个继承于NSObject类的类,在.h文件中 #import "AFHTTPRequestOperationManager.h" //定义两个block来接收请求成功和失 ...
随机推荐
- C# Ref 与out 的区别
在C#中,有四种传递参数方式: 1. 传值 (value) : 无额外修饰符 2. 传址(reference) : 需修饰符Ref,传入函数的参数必须先赋值 3. 输出参数(output): 需修饰符 ...
- ubuntu1404安装配置java环境(jdk8)
这个安装比较简单,网上也有数不清的教学,我这里记录以下,方便以后万一失忆了回来看看能想起来.个人博客http://www.cnblogs.com/wdfwolf3/ 1.下载安装 言归正传,我们需要到 ...
- 【elasticsearch】(4)centos7 超简单安装elasticsearch 的 jdbc
前言 elasticsearch(下面简称ES)使用jdbc连接mysql比go-mysql-elasticsearch的elasticsearch-river-jdbc能够很好的支持增量数据更新的问 ...
- npm install express -g出错
npm ERR! Windows_NT npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program ...
- php根据经纬度计算距离和方向--摘录自http://haotushu.sinaapp.com/post-520.html
define('EARTH_RADIUS', 6367000);//需定义的静态变量 function getRadian($d) { return $d * M_PI / 180; } functi ...
- InstallShield:自己备份
LIST listData;//声明listData listData = ListCreate(STRINGLIST);//创建一个空的实际字符串或数字列表. //参数都是在上个界面中赋值,然后在下 ...
- 利用def生成dll文件
DLL中导出函数的声明有两种方式:一种为在函数声明中加上__declspec(dllexport),这里不再举例说明:另外一种方式是采用模块定义(.def) 文件声明,.def文件为链接器提供了有关被 ...
- 鸟哥的linux私房菜——第12章 正则表达式与文件格式化处理
12.1什么是正则表达式 正则表达式就是处理字符串的方法,它是以行为单位来进行字符串的处理行为,正则表达式通过一些特殊符号的辅助,可以让用户轻易达到查找.删除.替换某特定字符串的处理程序. vi.gr ...
- c++的四种强制类型转换
http://hb.qq.com/a/20110722/001452.htm ...... C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是:TYPE b = (TYPE)a ...
- 一种轻量的openresty路由设计
在使用openresty开发接口的过程会发现一个问题,那就是接口的地址问题怎么解决,最好一个接口地址对应一个lua文件,也可以在nginx.conf 配置中使用content_by_lua 来编写接口 ...