Many XDR-coded content was found on the web, but most of them were related and few programming examples. Because the distributed curriculum learns the XDR external data encoding and is applied in the implementation of the RPC remote Procedure call. This blog first briefly describes the XDR correlation, the next article will introduce the socket communication.
This blog describes the internal implementation of XDR http://blog.csdn.net/chdhust/article/details/9004496, need to understand the implementation can refer to.
The main role of XDR is to avoid data transfer receive exceptions due to inconsistent computer platforms when passing message parameters between different processes. It can encode the message parameters in a certain order, put them in a packet (usually in memory to request a certain size of the string buffer), and then send the packet to other platforms, and then in accordance with the previous encoding sequence decoding, and can get the original message parameters.
This blog post simply describes an XDR code instance http://blog.csdn.net/seulww/article/details/23093913
Environmental requirements:
Linux Operating System
Rpc/xdr.h
1#include <iostream>2#include <cstdio>3#include <malloc.h>4#include <rpc/xdr.h>5#include <cstring>6 using namespacestd;7 8 Char*func ()9 {Ten Char*res = (Char*)malloc(sizeof(Char)*1024x768); Onesprintf (Res,"Nihao, hello."); A returnRes; - } - the intMainintargcChar*argv[]) - { -printf"begin...\n"); - + ShortAge = the; - CharSex ='M'; + Doubleweights =66.66; A Charname[ -]; atstrcpy (Name,"LJ"); - Char*p =name; - - - XDR-XDR; - Charbuff[ -]; in -Xdrmem_create (&xdr,buff, -, Xdr_encode); toXdr_string (&xdr,&p,sizeofp); +Xdr_short (&xdr,&Age ); -Xdr_char (&xdr,&sex); theXdr_double (&xdr,&weights); * $Xdrmem_create (&xdr,buff, -, Xdr_decode);Panax Notoginseng Charname2[ -]; - Char*P2 =name2; theCout<<strlen (p2) <<Endl; +Xdr_string (&XDR,&P2,sizeofp2); A ShortAge2; theXdr_short (&xdr,&age2); + CharSex2; -Xdr_char (&xdr,&sex2); $ Doubleweights2; $Xdr_double (&xdr,&weights2); - -printf"%s%d%c%lf\n", p2,age2,sex2,weights2); the -memset (Buff,0,sizeofbuff);WuyiXdrmem_create (&xdr,buff,sizeofbuff,xdr_encode); the Char*P4 = (Char*)malloc(sizeof(Char)*1024x768); -P4 =func (); Wuprintf"P4 =%s\n", p4); -printf"sizeof P4 =%d\n",(int)(sizeofp4)); AboutXdr_string (&XDR,&P4,1024x768); $ -Xdrmem_create (&xdr,buff,sizeofbuff,xdr_decode); - Charstr2[1024x768]; - Char*P5 =str2; AXdr_string (&XDR,&P5,1024x768); + theprintf"str2 =%s\n", str2); - $ return 0; the } the the /*file* pFile = fopen ("In.txt", "ab+"); the XDR-XDR; - xdrstdio_create (&xdr,pfile,xdr_encode); in int n = 299; the Xdr_int (&xdr,&n); the Xdr_destroy (&XDR); About fclose (pFile); the */
Detailed
XDR-XDR; Define an XDR object
Xdrmem_create (&xdr,buff,120,xdr_encode); Create encoding mode, buff is a custom buffer, storing parameters, 120 is actually the size of the buff, Xdr_encode is encoded, the corresponding Xdr_decode decoding
Xdr_int (&xdr,&n); Encoded according to the parameter type Xdr_type (&xdr,&type);
It should be noted that the encoding of the string to be used xdr_string, and the argument of the reference & must be a variable, but not the array name of the string, which is the rule of the C language, although the array name is the first address of the array pointer, but is not a variable, cannot reference &.
Xdrmem_create (&xdr,buff,120,xdr_decode); The decoding process and coding process are put over, specific reference code
Note the green annotated part of the code:
Is the way XDR files are encoded, rather than using in-memory buffers to store information in a manner similar to that used.
List some common coding functions (from Xdr.h in Linux):
/*XDR using memory buffers*/extern voidXdrmem_create (XDR-*__xdrs,Constcaddr_t __addr, U_int __size,enumxdr_op __xop) __throw;/*XDR using Stdio library*/extern voidXdrstdio_create (XDR *__xdrs, FILE *__file,enumxdr_op __xop) __throw;/** These is the "generic" XDR routines. * None of these can have const applied because it's not possible to * know WH Ether The call was a read or a write to the passed parameter * Also, the XDR structure was always updated by some of these C Alls. */externbool_t Xdr_void (void) __throw;externbool_t Xdr_short (XDR-*__xdrs, Short*__sp) __throw;externbool_t Xdr_u_short (XDR *__xdrs, U_short *__usp) __throw;externbool_t Xdr_int (XDR-*__xdrs,int*__ip) __throw;externbool_t Xdr_u_int (XDR *__xdrs, U_int *__up) __throw;externbool_t Xdr_long (XDR-*__xdrs,Long*__lp) __throw;externbool_t Xdr_u_long (XDR *__xdrs, U_long *__ulp) __throw;externbool_t Xdr_hyper (XDR *__xdrs, quad_t *__LLP) __throw;externbool_t Xdr_u_hyper (XDR *__xdrs, u_quad_t *__ULLP) __throw;externbool_t xdr_longlong_t (XDR *__xdrs, quad_t *__LLP) __throw;externbool_t xdr_u_longlong_t (XDR *__xdrs, u_quad_t *__ULLP) __throw;externbool_t xdr_int8_t (XDR *__xdrs, int8_t *__ip) __throw;externbool_t xdr_uint8_t (XDR *__xdrs, uint8_t *__up) __throw;externbool_t xdr_int16_t (XDR *__xdrs, int16_t *__ip) __throw;externbool_t xdr_uint16_t (XDR *__xdrs, uint16_t *__up) __throw;externbool_t xdr_int32_t (XDR *__xdrs, int32_t *__ip) __throw;externbool_t xdr_uint32_t (XDR *__xdrs, uint32_t *__up) __throw;externbool_t xdr_int64_t (XDR *__xdrs, int64_t *__ip) __throw;externbool_t xdr_uint64_t (XDR *__xdrs, uint64_t *__up) __throw;externbool_t xdr_quad_t (XDR *__xdrs, quad_t *__ip) __throw;externbool_t xdr_u_quad_t (XDR *__xdrs, u_quad_t *__up) __throw;externbool_t Xdr_bool (XDR *__xdrs, bool_t *__bp) __throw;externbool_t Xdr_enum (XDR *__xdrs, enum_t *__ep) __throw;externbool_t Xdr_array (XDR * _xdrs, caddr_t *__ADDRP, U_int *__sizep, U_int __maxsize, U_int __elsize, xdrproc_t __elproc) __throw;externbool_t xdr_bytes (XDR-*__xdrs,Char**__cpp, U_int *__sizep, U_int __maxsize) __throw;externbool_t Xdr_opaque (XDR *__xdrs, caddr_t __cp, U_int __cnt) __throw;externbool_t xdr_string (XDR-*__xdrs,Char**__cpp, U_int __maxsize) __throw;externbool_t xdr_union (XDR *__xdrs, enum_t *__dscmp,Char*__UNP,Const structXdr_discrim *__choices, xdrproc_t __dfault) __throw;externbool_t Xdr_char (XDR-*__xdrs,Char*__cp) __throw;externbool_t Xdr_u_char (XDR *__xdrs, U_char *__cp) __throw;externbool_t Xdr_vector (XDR-*__xdrs,Char*__basep, U_int __nelem, U_int __elemsize, xdrproc_t __xdr_elem) __throw;externbool_t xdr_float (XDR-*__xdrs,float*__fp) __throw;externbool_t xdr_double (XDR-*__xdrs,Double*__dp) __throw;externbool_t xdr_reference (XDR *__xdrs, caddr_t *__xpp, U_int __size, xdrproc_t __proc) __throw;externbool_t Xdr_pointer (XDR-*__xdrs,Char**__objpp, U_int __obj_size, xdrproc_t __xdr_obj) __throw;externbool_t xdr_wrapstring (XDR-*__xdrs,Char**__cpp) __throw;externU_long xdr_sizeof (xdrproc_t,void*) __throw;
Example of XDR external data encoding in Linux RPC