0

Say I've got a server and a client.

Server

I have some numerical data that I generate below via SerializeDebug which returns a std::vector<uint8_t>. I then convert this to a python array via convert. Then, in python, I send the data over a socket to the client.

Client

I receive the data in Python and pass it to the other end of the numerical computation library for deserialization.

Code

The C++ code is as follows

using ByteThing = std::vector<uint8_t>;

ByteThing SerializeDebug(){
  ByteThing toRet(240, 0);
  for (int i=0; i < 240; i++){
      toRet[i] += i;
  }
  return toRet;
};
void DeserializeDebug(const boost::python::list &pyvals){
  std::cout << "DEBUGGING DEBUG MESSAGE: \t";
  for (unsigned int i = 0; i < len(pyvals); i++) {
      std::cout << boost::python::extract<uint8_t>(pyvals[i]) << " ,";
  }
}

static PyObject *convert(const ByteString &vector) {
  auto *pythonList = new boost::python::list();
  for (auto &el: vector){
      pythonList->append(el);
  }
  return pythonList->ptr();
}

The python code for sending and receiving the data looks like:

def _safe_send_unencrypted_obj(socket: Socket, obj: Any) -> None:
    socket.sendall(_add_header_to_payload(dill.dumps(obj)))
    while True:
        logger.debug("_safe_send_unenc_obj: Waiting for confirmation")
        res = socket.recv(BUFFER_SIZE)
        if res:
            logger.debug("Receipt confirmed from other party")
            break

def _safe_recv_unencrypted_obj(socket: Socket):
    expected_length = _get_obj_size(socket)
    logger.debug(f"_safe_recv_unenc_obj: Expected size: {expected_length}")
    running_length = 0
    running_msg = b""
    while running_length < expected_length:
        msg = socket.recv(BUFFER_SIZE)
        if msg:
            running_length += len(msg)
            running_msg += msg
    socket.send(_add_header_to_payload(b"ACK"))
    logger.debug("_safe_recv_unenc_obj: Received all data")
    return running_msg

def server_run():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(
        socket.SOL_SOCKET,
        socket.SO_REUSEADDR,
        1,
    )
    s.bind((socket.gethostname(), ADDRESS))
    s.listen(5)
    clientsocket, address = s.accept()
    logger.info("Accepted connection")

    interface = pycrypto.CKKSwrapper()
    data = interface.SerializeDebug()

    _safe_send_unencrypted_obj(clientsocket, obj=data)

def client_run(crannog_parameters):
    serversocket: Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serversocket.connect((socket.gethostname(), ADDRESS))
    serversocket.setblocking(True)

    resp = _safe_recv_unencrypted_obj(serversocket)
    resp: List[int] = dill.loads(resp)

    interface = pycrypto.CKKSwrapper()
    interface.DeserializeDebug(list(resp))

Server Results

CPP byte thing -> Python is correct. I've opened the debugger and verified that the values are as expected.

CPP vector<uint8_t>: [0, 1, ..., 240]

python List[int]: [0, 1, ..., 240]

I then send it to the client

Client Results

After unpickling, I get the expected results (as checked in the debugger)

At this point, I have an array of length 240: [0, 1, ..., 240]

When I call interface.DeserializeDebug(resp), I get

DEBUGGING DEBUG MESSAGE:      , , , , , , ,  ,   ,
 , , , , , , , , , , , , , , , , , , ,  ,! ," ,# ,$ ,% ,& ,' ,( ,) ,* ,+ ,, ,- ,. ,/ ,0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,: ,; ,< ,= ,> ,? ,@ ,A ,B ,C ,D ,E ,F ,G ,H ,I ,J ,K ,L ,M ,N ,O ,P ,Q ,R ,S ,T ,U ,V ,W ,X ,Y ,Z ,[ ,\ ,] ,^ ,_ ,` ,a ,b ,c ,d ,e ,f ,g ,h ,i ,j ,k ,l ,m ,n ,o ,p ,q ,r ,s ,t ,u ,v ,w ,x ,y ,z ,{ ,| ,} ,~ , ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,� ,

Does anyone have any thoughts as to why this is going wrong?

IanQ
  • 1,831
  • 5
  • 20
  • 29
  • 1
    I think you question maybe boils down to being the same as: https://stackoverflow.com/questions/19562103/uint8-t-cant-be-printed-with-cout – PeterSW Feb 11 '21 at 00:07
  • In your "Client Results" section you're basically saying the client server part all worked fine but there's a bug in the `DeserializeDebug` method. – PeterSW Feb 11 '21 at 00:14
  • Gotcha! Thanks. I assumed I was making a mistake and that while sending the sockets some data was getting mangled – IanQ Feb 11 '21 at 01:36

0 Answers0