首页 > 解决方案 > 通过套接字 C++ 发送指针

问题描述

我有一个 C++ 客户端/服务器设置,我需要uint8_t*在它们之间传输一个指针(特别是 )。

编辑:指针指向的缓冲区

使用传统的send/recv似乎不起作用(需要 a char*),理想情况下,我会将此指针作为带有其他一些变量的数据包的一部分发送。

这样做的最佳方法是什么?

服务器端来说明:

    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;
        }

标签: c++socketspointers

解决方案


推荐阅读