Linux学习笔记31——网络信息
一 主机数据库函数
#include <netdb.h>
struct hostent *gethostbyaddr(const void *addr, //地址
size_t len, //长度
int type //类型
);
struct hostent *gethostbyname(const char *name);
这些函数返回的结构中至少包含以下几个成员
struct hostent{
char *h_name; //主机名称
char **h_aliases; //别名列表
int h_addrtype; //地址类型
int h_length; //地址长度
char **h_addr_list; //地址列表
};
如果想获得某台计算机的主机数据库信息,可以调用gethostbyname函数并且将结果打印出来,注意,要把返回的地址列表转换为正确的地址类型,并用函数inet_ntoa将它们从网络字节序转换为可打印的字符串
#include <arpa/inet.h>
char *inet_ntoa(struct in_addr in);
函数作用:将一个因特网主机地址转换为一个点分四元组格式的字符串
#include <unistd.h>
int gethostname(char *name,int namelength);
函数作用:将当前主机的名字写入name指向的字符串中。主机名为null结尾。参数namelength指定了字符串name的长度,如果返回的主机名太长,它就会被截断
例子:
#include <stdio.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h> int main(int argc,char *argv[]){
char *host,**names,**addrs;
struct hostent *hostinfo;
//把host变量设置为getname程序所提供的命令行参数,或默认设置为用户主机的主机名
if(argc==){
char myname[];
gethostname(myname,);
host=myname;
}else{
host=argv[];
}
//调用gethostbyname,如果未找到相应的信息就报告一条错误
hostinfo=gethostbyname(host);
if(!hostinfo){
sprintf(stderr,"Cannot get info for host:%s\n",host);
exit();
}
//显示主机名和它可能有的所有别名
printf("result for host:%s\n",host);
printf("Name:%s\n",hostinfo->h_name);
printf("Aliases:");
names=hostinfo->h_aliases;
while(*names){
printf("%s",*names);
names++;
}
printf("\n");
if(hostinfo->h_addrtype!=AF_INET){
fprintf(stderr,"not an IP host!\n");
exit();
}
addrs=hostinfo->h_addr_list;
while(*addrs){
printf("%s",inet_ntoa(*(struct in_addr*)*addrs));
addrs++;
}
printf("\n");
exit();
}
二 服务信息函数
#include <netdb.h>
struct servent *getservbyname(const char *name, //服务名称
const char *proto //指定用于连接该服务的协议,它的取值是tcp(用于SOCK_SREAM类型的TCP连接)和udp(用于SOCK_DGRAM类型的UPD数据报)
);
struct servent *getservbyport(int port, //端口号
const char *proto
);
结构servent至少包含一下几个成员
struct servent{
char *s_name; //服务名称
char **s_aliases; //别名列表
int s_port; //IP端口号
char *s_proto; //服务类型
};
例子:
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> int main(int argc,char *argv[]){
char *host;
int sockfd;
int len,result;
struct sockaddr_in address;
struct hostent *hostinfo;
struct servent *servinfo;
char buffer[]; if(argc==){
host="localhost";
}else{
host=argv[];
}
//查找主机的地址,如果找不到,就报告一条错误
hostinfo=gethostbyname(host);
if(!hostinfo){
fprintf(stderr,"no host:%s\n",host);
exit();
}
//检查主机上是否有daytime服务
servinfo=getservbyname("daytime","tcp");
if(!servinfo){
fprintf(stderr,"no daytime service\n");
exit();
}
printf("daytime port is %d\n",ntohs(servinfo->s_port));
//创建一个套接字
sockfd=socket(AF_INET,SOCK_STREAM,);
//构造connect调用要使用的地址
address.sin_family=AF_INET;
address.sin_port=servinfo->s_port;
address.sin_addr=*(struct in_addr*)*hostinfo->h_addr_list;
len=sizeof(address);
//然后建立连接并取得有关信息
result=connect(sockfd,(struct sockaddr *)&address,len);
if(result==-){
perror("oops:getdate");
exit();
}
result=read(sockfd,buffer,sizeof(buffer));
buffer[result]='\0';
printf("read %d bytes:%s",result,buffer);
close(sockfd);
exit();
}
Linux学习笔记31——网络信息的更多相关文章
- kali linux学习笔记(四) : 网络端口大全介绍
端口大全介绍 2端口:管理实用程序 3端口:压缩进程 5端口:远程作业登录 7端口:回显 9端口:丢弃 11端口:在线用户 13端口:时间 17端口:每日引用 18端口:消息发送协议 19端口:字符发 ...
- python3.4学习笔记(十三) 网络爬虫实例代码,使用pyspider抓取多牛投资吧里面的文章信息,抓取政府网新闻内容
python3.4学习笔记(十三) 网络爬虫实例代码,使用pyspider抓取多牛投资吧里面的文章信息PySpider:一个国人编写的强大的网络爬虫系统并带有强大的WebUI,采用Python语言编写 ...
- Linux 学习笔记
Linux学习笔记 请切换web视图查看,表格比较大,方法:视图>>web板式视图 博客园不能粘贴图片吗 http://wenku.baidu.com/view/bda1c3067fd53 ...
- linux学习笔记2-linux的常用命令
第一篇博客:linux学习笔记1-ubuntu的安装与基本设置 之中,已经介绍了如何安装linux操作系统,以及一些基本的设置修改. 本篇博客主要介绍linux中的一些常用的终端命令 ======== ...
- Linux学习笔记-林耐斯Notes-Linux就该这么学
Linux学习笔记... 参考的优秀Linux网站: http://www.w3cschool.cn/linux/ http://www.linuxeye.com/ http://linux.vbir ...
- deepin linux学习笔记
目录 deepin linux学习笔记 前言 linux常用命令 ls 显示文件夹内容 cd 切换当前目录 pwd 查看当前工作目录 mkdir 新建文件夹 rm 删除文件或文件夹 mv 移动文件 c ...
- linux学习笔记2 - linux常用命令
转载请标注原链接:http://www.cnblogs.com/xczyd/p/5543731.html 第一篇博客:linux学习笔记1-ubuntu的安装与基本设置 之中,已经介绍了如何安装lin ...
- Linux 学习笔记之超详细基础linux命令(the end)
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 14---------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 14
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 13---------------- ...
随机推荐
- 滑动条 Trackbar[OpenCV 笔记9]
OpenCV中没有实现按钮的功能,我们可以利用滑动条来实现按钮功能. , ); trackbarname 轨迹条的名字. winname 窗口的名字,轨迹条会依附在这个窗口上. value 一个指向整 ...
- python的一些学习资料(持续更新中)
Markdown在线编辑器 廖雪峰官方博客[基础入门好资料] python-guide[传说中的巨牛写的] the5fire的技术博客[全职python程序员博客]
- 使用css3实现文章新闻列表排行榜(数字)
列举几个简单的文章排行榜数字效果 一:使用list-style来显示数字.圆点.字母或者图片 <style> li{width:300px; border-bottom: 1px dott ...
- Linux命令 &与&&的作用
1.ls &表示后台服务 2.ls && ll 表示前者执行成功,执行后台命令
- TDirectory.GetFileSystemEntries获取指定目录下的目录和文件
使用函数: System.IOUtils.TDirectory.GetFileSystemEntries 所有重载: class function GetFileSystemEntries(const ...
- Presto: 可以处理PB级别数据的分布式SQL查询引擎
2012年秋季Facebook启动了Presto,Presto的目的是在几百PB级别数据量上面进行准实时分析.在摒弃了一些外部项目以后,Facebook准备开发他们自己的分布式查询引擎.Presto的 ...
- sql 判断一个表的数据不在另一个表中
SELECT a.* FROM a LEFT JOIN b ON a.key = b.key WHERE (b.key IS NULL) end as flag from a select id fr ...
- jQuery获取屏幕的宽度
Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.b ...
- Codeforces Round #198 (Div. 2) —— B
B题是一个计算几何的题,虽然以前看过计算几何的ppt,但一直都没有写过: 昨晚比赛的时候本来想写的,但是怕不熟练浪费时间,太可惜了! 其实没必要选出一个最大的矩形: 以矩形的一条对角线为轴,向上或者向 ...
- AFNetworking网络请求的get和post步骤
1.首先通过第三方:CocoaPods下载AFNetworking 1.1.先找到要查找的三方库:pod search + AFNetworking 1.2.出来一堆列表页面,选择三方库最新版本命 ...