Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5083067
  • 博文数量: 553
  • 博客积分: 13864
  • 博客等级: 上将
  • 技术积分: 11041
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-28 21:25
个人简介

个人Blog: hhktony.com

文章分类

全部博文(553)

文章存档

2015年(1)

2014年(2)

2013年(12)

2012年(384)

2011年(154)

分类: LINUX

2011-11-29 02:05:43

                      这一章学习转换和网络方面的信息函数

3.1 字节转换函数

在网络上面有着许多类型的机器,这些机器在表示数据的字节顺序是不同的, 比如i386芯片是低字节在内存地址的低端,高字节在高端,而alpha芯片却相反. 为了统一起来,在Linux下面,有专门的字节转换函数. 
unsigned long int htonl(unsigned long int hostlong)
unsigned short int htons(unisgned short int hostshort)
unsigned long int ntohl(unsigned long int netlong)
unsigned short int ntohs(unsigned short int netshort)

在这四个转换函数中,h 代表host, n 代表 network.   s 代表short l 代表long 
第一个函数的意义是将本机器上的long数据转化为网络上的long. 其他几个函数的意义也差不多.

3.2 IP和域名的转换


在网络上标志一台机器可以用IP或者是用域名.那么我们怎么去进行转换呢?

struct hostent *gethostbyname(const char *hostname)
struct hostent *gethostbyaddr(const char *addr,int len,int type)
在中有struct hostent的定义
struct hostent{
char *h_name; /* 主机的正式名称 */
char *h_aliases; /* 主机的别名 */
int h_addrtype; /* 主机的地址类型 AF_INET*/
int h_length; /* 主机的地址长度 对于IP4 是4字节32位*/
char **h_addr_list; /* 主机的IP地址列表 */
}
#define h_addr h_addr_list[0] /* 主机的第一个IP地址*/

gethostbyname可以将机器名(如 linux.yessun.com)转换为一个结构指针.在这个结构里面储存了域名的信息

gethostbyaddr可以将一个32位的IP地址(C0A80001)转换为结构指针. 


这两个函数失败时返回NULL 且设置h_errno错误变量,调用h_strerror()可以得到详细的出错信息


3.3 字符串的IP和32位的IP转换.
在网络上面我们用的IP都是数字加点(192.168.0.1)构成的, 而在struct in_addr结构中用的是32位的IP,   我们上面那个32位IP(C0A80001)是的192.168.0.1 为了转换我们可以使用下面两个函数 

#include
#include
#include
int inet_aton(const char *cp,struct in_addr *inp)
char *inet_ntoa(struct in_addr in)

#include
int inet_pton(int af, const char *src, void *dst);
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);

函数里面 a 代表 ascii  n 代表network.第一个函数表示将a.b.c.d的IP转换为32位的IP,存储在 inp指针里面.第二个是将32位IP转换为a.b.c.d的格式. 

3.4 服务信息函数
在网络程序里面我们有时候需要知道端口.IP和服务信息.这个时候我们可以使用以下几个函数

int getsockname(int sockfd,struct sockaddr *localaddr,int *addrlen)
int getpeername(int sockfd,struct sockaddr *peeraddr, int *addrlen)
struct servent *getservbyname(const char *servname,const char *protoname)
struct servent *getservbyport(int port,const char *protoname)
struct servent
{
char *s_name; /* 正式服务名 */
char **s_aliases; /* 别名列表 */
int s_port; /* 端口号 */
char *s_proto; /* 使用的协议 */
}

一般我们很少用这几个函数.对应客户端,当我们要得到连接的端口号时在connect调用成功后使用可得到 系统分配的端口号.对于服务端,我们用INADDR_ANY填充后,为了得到连接的IP我们可以在accept调用成功后 使用而得到IP地址. 
在网络上有许多的默认端口和服务,比如端口21对ftp80对应WWW.为了得到指定的端口号的服务 我们可以调用第四个函数,相反为了得到端口号可以调用第三个函数. 

3.5 一个例子

点击(此处)折叠或打开
  1. /*
  2.  * =====================================================================================
  3.  *
  4.  * Filename: getopt_long.c
  5.  *
  6.  * Description:
  7.  *
  8.  * Version: 1.0
  9.  * Created: 2012年04月16日 20时35分39秒
  10.  * Revision: none
  11.  * Compiler: gcc
  12.  *
  13.  * Author: XuTao (xt), butbueatiful@gmial.com
  14.  * Company: akaedu.org
  15.  *
  16.  * =====================================================================================
  17.  */

  18. /*编译使用gcc -o getopt_long getopt_long.c*/
  19. #include <getopt.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>

  22. /*程序的名字*/
  23. const char *program_name;

  24. /* 打印程序参数 */
  25. void print_usage(FILE * stream, int exit_code)
  26. {
  27.     fprintf(stream, "Usage: %s options [ inputfile ... ]\n", program_name);
  28.     fprintf(stream,
  29.      " -h --help 显示这个帮助信息.\n"
  30.      " -o --output filename         将输出定位到文件.\n"
  31.      " -v --version     打印版本信息.\n");
  32.     exit(exit_code);
  33. }


  34. /* 主程序 */
  35. int main(int argc, char *argv[])
  36. {
  37.     int next_option;        //下一个要处理的参数符号
  38.     int haveargv = 0;        //是否有我们要的正确参数,一个标识

  39. /* 包含短选项字符的字符串,注意这里的‘:’ */

  40.     const char *const short_options = "ho:v";

  41. /* 标识长选项和对应的短选项的数组 */

  42.     const struct option long_options[] = {
  43.     {"help", 0, NULL, 'h'},
  44.     {"output", 1, NULL, 'o'},
  45.     {"version", 0, NULL, 'v'},
  46.     {NULL, 0, NULL, 0}
  47.     };                //最后一个元素标识为NULL

  48.     /* 此参数用于承放指定的参数,默认为空 */
  49.     const char *output_filename = NULL;
  50. /* 一个标志,是否显示版本号 */
  51.     int verbose = 0;

  52. /* argv[0]始终指向可执行的文件文件名 */

  53.     program_name = argv[0];

  54.     do {
  55.     next_option =
  56.      getopt_long(argc, argv, short_options, long_options, NULL);
  57.     switch (next_option) {
  58.     case 'h':        /* -h or --help */
  59.      haveargv = 1;
  60.      print_usage(stdout, 0);
  61.     case 'o':        /* -o or --output */
  62.      /* 此时optarg指向--output后的filename */
  63.      output_filename = optarg;
  64.      haveargv = 1;
  65.      break;
  66.     case 'v':        /* -v or --version */
  67.      verbose = 1;
  68.      haveargv = 1;
  69.      break;
  70.     case ':':        /* 缺乏长选项内容 */
  71.      break;
  72.     case '?':        /* 出现一个未指定的参数 */
  73.      print_usage(stderr, 1);
  74.     case -1:        /* 处理完毕后返回-1 */
  75.      if (!haveargv) {
  76.         print_usage(stderr, 1);
  77.      }
  78.      break;
  79.     default:        /* 未指定的参数出现,出错处理 */
  80.      print_usage(stderr, 1);
  81.      break;
  82.     }
  83.     } while (next_option != -1);

  84.     if (verbose) {
  85.     int i;
  86.     for (i = optind; i < argc; ++i)
  87.      printf("Argument: %s\n", argv[i]);
  88.     }

  89.     return 0;
  90. }
在这个例子里面,为了判断用户输入的是IP还是域名我们调用了两个函数,第一次我们假设输入的是IP所以调用inet_aton,
失败的时候,再调用gethostbyname而得到信息.
阅读(1443) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~