0

I have a C++ client/server set up and I need to transfer a pointer (specifically, uint8_t*) between them.

EDIT: the buffer that the pointer points to

Using the traditional send/recv does not appear to work (requires a char*), and ideally I would send this pointer as part of a packet with some other variables.

What is the best way of doing this?

Server side to illustrate:

    char recvbuf[DEFAULT_BUFLEN];
    int recvbuflen = DEFAULT_BUFLEN;

    int iResult = recv( this->ClientSocket, recvbuf, recvbuflen, 0 );
    if( iResult > 0 )
    {
        // ideally recvbuf would contain the pointer
        // Do some stuff

        // Send the buffer back to the sender
        std::string result = "Message received";
        // ideally here I would be sending my pointer, not this string
        int iSendResult = send( this->ClientSocket, result.c_str(), (int)strlen( result.c_str() ), 0 );
        if( iSendResult == SOCKET_ERROR )
        {
            printf( "send failed with error: %d\n", WSAGetLastError() );
            closesocket( this->ClientSocket );
            WSACleanup();
            return false;
        }
Jessica Chambers
  • 1,246
  • 5
  • 28
  • 56
  • 2
    Wait, do you want to send the *pointer address* or a buffer that’s pointed to by the pointer? – Konrad Rudolph Sep 21 '20 at 14:47
  • Why? Isn't it going to be unreadable at the source anyway? – user202729 Sep 21 '20 at 14:47
  • You want to send the data the pointer points to, not the pointer itself. sending an address from one machine to another isn't going to get you anything as all OS'es randomize the process address space. – NathanOliver Sep 21 '20 at 14:49
  • You cannot reasonably send a pointer over a socket because the pointer value doesn't mean anything on the target machine. Obviously you can send whatever the pointer is pointing to, is that what you want? – john Sep 21 '20 at 14:50
  • 2
    What do you imagine that the receiving program would be able to use that pointer for? – molbdnilo Sep 21 '20 at 14:53
  • Just cast to char* – user253751 Sep 23 '20 at 18:23

0 Answers0