一 主机数据库函数

#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——网络信息的更多相关文章

  1. kali linux学习笔记(四) : 网络端口大全介绍

    端口大全介绍 2端口:管理实用程序 3端口:压缩进程 5端口:远程作业登录 7端口:回显 9端口:丢弃 11端口:在线用户 13端口:时间 17端口:每日引用 18端口:消息发送协议 19端口:字符发 ...

  2. python3.4学习笔记(十三) 网络爬虫实例代码,使用pyspider抓取多牛投资吧里面的文章信息,抓取政府网新闻内容

    python3.4学习笔记(十三) 网络爬虫实例代码,使用pyspider抓取多牛投资吧里面的文章信息PySpider:一个国人编写的强大的网络爬虫系统并带有强大的WebUI,采用Python语言编写 ...

  3. Linux 学习笔记

    Linux学习笔记 请切换web视图查看,表格比较大,方法:视图>>web板式视图 博客园不能粘贴图片吗 http://wenku.baidu.com/view/bda1c3067fd53 ...

  4. linux学习笔记2-linux的常用命令

    第一篇博客:linux学习笔记1-ubuntu的安装与基本设置 之中,已经介绍了如何安装linux操作系统,以及一些基本的设置修改. 本篇博客主要介绍linux中的一些常用的终端命令 ======== ...

  5. Linux学习笔记-林耐斯Notes-Linux就该这么学

    Linux学习笔记... 参考的优秀Linux网站: http://www.w3cschool.cn/linux/ http://www.linuxeye.com/ http://linux.vbir ...

  6. deepin linux学习笔记

    目录 deepin linux学习笔记 前言 linux常用命令 ls 显示文件夹内容 cd 切换当前目录 pwd 查看当前工作目录 mkdir 新建文件夹 rm 删除文件或文件夹 mv 移动文件 c ...

  7. linux学习笔记2 - linux常用命令

    转载请标注原链接:http://www.cnblogs.com/xczyd/p/5543731.html 第一篇博客:linux学习笔记1-ubuntu的安装与基本设置 之中,已经介绍了如何安装lin ...

  8. Linux 学习笔记之超详细基础linux命令(the end)

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 14---------------- ...

  9. Linux 学习笔记之超详细基础linux命令 Part 14

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 13---------------- ...

随机推荐

  1. 在jsp中选中checkbox后 将该记录的多个数据获取,然后传到Action类中进行后台处理 双主键情况下 *.hbm.xml中的写法

    在jsp中选中checkbox后 将该记录的多个数据获取,然后传到Action类中进行后台处理 双主键情况下 *.hbm.xml中的写法   ==========方法1: --------1. 选相应 ...

  2. Spring 对JDBC的支持(JdbcTemplate)

    Spring对数据库的操作,使用JdbcTemplate对象 需要引入相关的jar文件 如版本:(Spring核心jar包就不列了) spring-jdbc-3.2.5.RELEASE.jar spr ...

  3. HDU 3681 Prison Break(状态压缩dp + BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...

  4. [C#]Thread Safe Dictionary in .NET 2.0

    using System.Collections.Generic; using System.Threading; namespace CSharpUtilHelpV2 { /// <summa ...

  5. 将选择的图片显示在listview中,并显示filename,path和type

    if (openFileDialog1.ShowDialog() == DialogResult.OK) { listView1.Items.Clear(); string[] files = ope ...

  6. C语言学习笔记——堆和栈——未整理

    C语言笔记     栈区     栈stack是一种先进后出的内存结构,所有的自动变量,函数的形参都是由编译器自动放出栈中,当一个自动变量超出其作用域时,自动从栈中弹出.出入栈是由C语言编译器自动分配 ...

  7. iphone 屏幕投射到Mac上

    在实际的工作中,我们往往需要演示iPhone上面的程序,但是由于手机屏幕太小,无法同时给很多人看,这时候就需要进行屏幕投射.目前我需要实现的是投射到Mac上.我使用有线USB和无线Airplay两种方 ...

  8. 动态网页技术---JSP

    JSP(全称JavaServer Pages)是由Sun Microsystems公司倡导和许多公司参与共同创建的一种使软件开发者可以响应客户端请求,而动态生成HTML.XML或其他格式文档的Web网 ...

  9. adb logcat 查看日志

    使用 logcat 命令 查看和跟踪系统日志缓冲区的命令logcat的一般用法是: [adb] logcat [<option>] ... [<filter-spec>] .. ...

  10. ubuntu 安装mysql及修改编码

    643  netstat -tap | grep mysql  645  apt-get install mysql-server mysql-client  646  netstat -tap | ...