0

I need to send big fixed sized buffers from python to c/c++ code side. From python side I have buffers in form of:

array = np.zeros([1000], dtype=np.uint8)
array = fill(array)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 1010))

Code below is used to recv data from connected socket from C/C++ side.From the c/c++ side I need to have this buffer as raw 1000 byte array, I don't need any headers, I need just data. Unfortunately I haven't found howto do it. In general np.tofile()- makes things I need but in file.

int recvBuffer(uint8_t *p_buff, uint32_t size, int socket)
{
    uint32_t recived = 0;
    uint8_t *p_curr = p_buff;
    while (recived < size)
    {
        int ret = recv(tcp_sock, p_curr, size - recived, 0);
        if (ret != -1)
        {
            p_curr += ret;
            recived += ret;
        }
        else
        {
            return false;
        }
    }
    return true;
}

  • What did you try to make your `np.array` raw? I do not think C/C++ magically distinguish numpy data and convert them into integers. I think you need to explicitly serialize it to bytes or (I have not tried but) try `ctypes`. – ghchoi Jan 23 '21 at 11:54
  • By raw I mean C-like array stype. np.arra(...,dtype =np.uint8) - internally is array of uint8_t values. – PavalGaenko Jan 25 '21 at 08:41
  • I do not think so. `np.array` is higher-order object. E.g., does not it have `.shape` property? It contains numeric values and much more for efficiency and effectiveness. You cannot directly send such things unless NumPy C API supports de-serializatoin (I do not know about this but it seems not promising). So you would better serialize your data. `[# row : int, # columns : int, type of numeric, numeric, ..., numeric]`. If sparse, serialization should be done in a different manner. – ghchoi Jan 25 '21 at 08:56
  • https://numpy.org/devdocs/reference/generated/numpy.load.html – ghchoi Jan 25 '21 at 09:05

1 Answers1

0

I found solution. I found it in np.tofile description.

np_array = np.zeros([1000], dtype=np.uint8)
np_array = fill(np_array)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 1010))
raw_buffer = np_array.to_bytes()
s.send(raw_buffer)

Makes exactlty what I wanted. raw_buffer - holds np.array buffer as raw c-stype buffer.