c++ - 通过套接字 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;
}
解决方案
推荐阅读
- regex - xsd 文件中的 html(s) 正则表达式验证
- ubuntu - 无法在ubuntu中下载腻子
- angular - 从 Angular 中的数组数据创建表
- sql - ORA-01810: 格式代码在 SQL 中出现两次
- arm - Cortex-A8 乱序执行和 Spectre
- python - 为什么 numpy 会保留这么多未使用的内存?
- macos - 全新安装monogame,如何解决警告
- performance - 如何在没有循环且时间复杂度较低的情况下从数组列表中删除一些项目?
- angular - Webpack 已使用与 API 模式未知属性“优化”不匹配的配置对象进行初始化
- android - 警报管理器无法在android中的确切时间启动