网上找了很多XDR编码的内容,但是大多都是介绍相关的,很少有编程实例。因为分布式的课程学习了XDR外部数据编码,并应用在了RPC远程过程调用的实现中。本篇博客先暂时描述XDR相关,下一篇将介绍Socket通信。
这一篇博客介绍了XDR的内部实现 http://blog.csdn.net/chdhust/article/details/9004496 ,需要了解实现的可以参考一下。
XDR的主要作用就是在不同进程间传递消息参数时,避免因为计算机平台的不一致而导致数据传送接收异常。它可以对消息参数按照一定的顺序编码,放在一个数据包里(通常是在内存中申请一个一定大小的字符串缓冲区),然后把这个数据包发送给其他平台,然后在按照之前编码的顺序依次解码,并可以获得原来的消息参数。
这一篇博客简单的描述了一个XDR编码实例 http://blog.csdn.net/seulww/article/details/23093913
环境要求:
Linux 操作系统
rpc/xdr.h
1 #include<iostream> 2 #include<cstdio> 3 #include<malloc.h> 4 #include<rpc/xdr.h> 5 #include<cstring> 6 using namespace std; 7 8 char* func() 9 { 10 char *res = (char *)malloc(sizeof(char)*1024); 11 sprintf(res,"nihao,你好 \n"); 12 return res; 13 } 14 15 int main(int argc,char* argv[]) 16 { 17 printf("begin...\n"); 18 19 short age = 34; 20 char sex = ‘M‘; 21 double weights = 66.66; 22 char name[20]; 23 strcpy(name,"LJ"); 24 char *p = name; 25 26 27 XDR xdr; 28 char buff[120]; 29 30 xdrmem_create(&xdr,buff,120,XDR_ENCODE); 31 xdr_string(&xdr,&p,sizeof p); 32 xdr_short(&xdr,&age); 33 xdr_char(&xdr,&sex); 34 xdr_double(&xdr,&weights); 35 36 xdrmem_create(&xdr,buff,120,XDR_DECODE); 37 char name2[20]; 38 char *p2 = name2; 39 cout<<strlen(p2)<<endl; 40 xdr_string(&xdr,&p2,sizeof p2); 41 short age2; 42 xdr_short(&xdr,&age2); 43 char sex2; 44 xdr_char(&xdr,&sex2); 45 double weights2; 46 xdr_double(&xdr,&weights2); 47 48 printf("%s %d %c %lf\n",p2,age2,sex2,weights2); 49 50 memset(buff,0,sizeof buff); 51 xdrmem_create(&xdr,buff,sizeof buff,XDR_ENCODE); 52 char *p4 = (char *)malloc(sizeof(char)*1024); 53 p4 = func(); 54 printf("p4 = %s\n",p4); 55 printf("sizeof p4 = %d\n",(int)(sizeof p4)); 56 xdr_string(&xdr,&p4,1024); 57 58 xdrmem_create(&xdr,buff,sizeof buff,XDR_DECODE); 59 char str2[1024]; 60 char *p5 = str2; 61 xdr_string(&xdr,&p5,1024); 62 63 printf("str2 = %s\n",str2); 64 65 return 0; 66 } 67 68 /* FILE* pFile = fopen("in.txt","ab+"); 69 XDR xdr; 70 xdrstdio_create(&xdr,pFile,XDR_ENCODE); 71 int n = 299; 72 xdr_int(&xdr,&n); 73 xdr_destroy(&xdr); 74 fclose(pFile); 75 */
详解:
XDR xdr; // 定义一个xdr对象
xdrmem_create(&xdr,buff,120,XDR_ENCODE); // 创建编码模式,buff是自定义的缓冲区,存放参数,120其实是buff的大小,XDR_ENCODE是编码,相应的XDR_DECODE解码
xdr_int(&xdr,&n); // 根据参数类型进行编码 xdr_type(&xdr,&type);
需要注意的是,字符串的编码要用 xdr_string,并且引用&的参数必须是变量,而不能是字符串的数组名,这是C语言的规则,数组名虽然是数组的首地址指针,但是不是变量,不能引用&。
xdrmem_create(&xdr,buff,120,XDR_DECODE); // 解码过程和编码过程放过来,具体参考代码
注意代码中绿色被注释部分:
是xdr文件编码的方式,而不是使用内存中的缓冲区来存储信息,使用方式类似的。
列举一些常用的编码函数(来自Linux中xdr.h):
/* XDR using memory buffers */ extern void xdrmem_create (XDR *__xdrs, const caddr_t __addr, u_int __size, enum xdr_op __xop) __THROW; /* XDR using stdio library */ extern void xdrstdio_create (XDR *__xdrs, FILE *__file, enum xdr_op __xop) __THROW; /* * These are the "generic" xdr routines. * None of these can have const applied because it‘s not possible to * know whether the call is a read or a write to the passed parameter * also, the XDR structure is always updated by some of these calls. */ extern bool_t xdr_void (void) __THROW; extern bool_t xdr_short (XDR *__xdrs, short *__sp) __THROW; extern bool_t xdr_u_short (XDR *__xdrs, u_short *__usp) __THROW; extern bool_t xdr_int (XDR *__xdrs, int *__ip) __THROW; extern bool_t xdr_u_int (XDR *__xdrs, u_int *__up) __THROW; extern bool_t xdr_long (XDR *__xdrs, long *__lp) __THROW; extern bool_t xdr_u_long (XDR *__xdrs, u_long *__ulp) __THROW; extern bool_t xdr_hyper (XDR *__xdrs, quad_t *__llp) __THROW; extern bool_t xdr_u_hyper (XDR *__xdrs, u_quad_t *__ullp) __THROW; extern bool_t xdr_longlong_t (XDR *__xdrs, quad_t *__llp) __THROW; extern bool_t xdr_u_longlong_t (XDR *__xdrs, u_quad_t *__ullp) __THROW; extern bool_t xdr_int8_t (XDR *__xdrs, int8_t *__ip) __THROW; extern bool_t xdr_uint8_t (XDR *__xdrs, uint8_t *__up) __THROW; extern bool_t xdr_int16_t (XDR *__xdrs, int16_t *__ip) __THROW; extern bool_t xdr_uint16_t (XDR *__xdrs, uint16_t *__up) __THROW; extern bool_t xdr_int32_t (XDR *__xdrs, int32_t *__ip) __THROW; extern bool_t xdr_uint32_t (XDR *__xdrs, uint32_t *__up) __THROW; extern bool_t xdr_int64_t (XDR *__xdrs, int64_t *__ip) __THROW; extern bool_t xdr_uint64_t (XDR *__xdrs, uint64_t *__up) __THROW; extern bool_t xdr_quad_t (XDR *__xdrs, quad_t *__ip) __THROW; extern bool_t xdr_u_quad_t (XDR *__xdrs, u_quad_t *__up) __THROW; extern bool_t xdr_bool (XDR *__xdrs, bool_t *__bp) __THROW; extern bool_t xdr_enum (XDR *__xdrs, enum_t *__ep) __THROW; extern bool_t xdr_array (XDR * _xdrs, caddr_t *__addrp, u_int *__sizep, u_int __maxsize, u_int __elsize, xdrproc_t __elproc) __THROW; extern bool_t xdr_bytes (XDR *__xdrs, char **__cpp, u_int *__sizep, u_int __maxsize) __THROW; extern bool_t xdr_opaque (XDR *__xdrs, caddr_t __cp, u_int __cnt) __THROW; extern bool_t xdr_string (XDR *__xdrs, char **__cpp, u_int __maxsize) __THROW; extern bool_t xdr_union (XDR *__xdrs, enum_t *__dscmp, char *__unp, const struct xdr_discrim *__choices, xdrproc_t __dfault) __THROW; extern bool_t xdr_char (XDR *__xdrs, char *__cp) __THROW; extern bool_t xdr_u_char (XDR *__xdrs, u_char *__cp) __THROW; extern bool_t xdr_vector (XDR *__xdrs, char *__basep, u_int __nelem, u_int __elemsize, xdrproc_t __xdr_elem) __THROW; extern bool_t xdr_float (XDR *__xdrs, float *__fp) __THROW; extern bool_t xdr_double (XDR *__xdrs, double *__dp) __THROW; extern bool_t xdr_reference (XDR *__xdrs, caddr_t *__xpp, u_int __size, xdrproc_t __proc) __THROW; extern bool_t xdr_pointer (XDR *__xdrs, char **__objpp, u_int __obj_size, xdrproc_t __xdr_obj) __THROW; extern bool_t xdr_wrapstring (XDR *__xdrs, char **__cpp) __THROW; extern u_long xdr_sizeof (xdrproc_t, void *) __THROW;