0

I am trying to serialize a structure in C using XDR and send the serialized data to python over a tcp socket.

I tried using xdrmem_create() to create an XDR stream , call appropriate pack functions and pass the character array to a socket, to achieve this but I get an EOF error on the python side when I try to deserialize the stream .

I was able to achieve this operation successfully between

C server and C client, also between

python server and python client.

I get an error only when I use it with C and Python.

C snippet

#include<rpc/rpc.h>
....
  xdrmem_create(&xdrs, arr, MAXLENGTH, XDR_ENCODE);

  if(!xdr_person(&xdrs,&pkt)){
    printf("ERROR");
  };
.....
send(new_fd, arr, MAXLENGTH, 0)

Python snippet

import xdrlib
.....
data = s.recv(4)

unpacker = xdrlib.Unpacker(data)
message_size = unpacker.unpack_uint()

data = s.recv(message_size)

unpacker.reset(data)

person={}

person["name"] = unpacker.unpack_string().decode()
person["age"] = unpacker.unpack_int()
person["flag"] = unpacker.unpack_bool()
person["errEnum"] = unpacker.unpack_enum()

I wonder if there is a mismatch between the way data is serialized in C and Python.

Eddy
  • 1
  • 2
  • First thing to check: `s.recv(n)` collects _up to_ `n` bytes from the socket, but it is allowed to collect less than `n`. Your program must be prepared to loop until it has collected the full amount of data required by the XDR blob that you intend to decode. That includes the 4 bytes for `message_size` -- it's possible, although unlikely, that that `recv` could return fewer than 4 bytes. To quickly see if a short `recv` is the cause of the problem, leave the program as is for now and print `len(data)` after each `recv`. – ottomeister Jun 14 '18 at 20:00
  • i checked and i am able to receive specified amount of bytes , I think this maybe an issue with XDR records . Do you have any idea how to use xdrrec_create()? i am not clear on what is expected from the readit() / writeit() functions. – Eddy Jun 15 '18 at 07:34

0 Answers0