gpt4 book ai didi

c - openssl ssl_read 将读取的二进制数据发送到 char*

转载 作者:行者123 更新时间:2023-11-30 17:42:31 25 4
gpt4 key购买 nike

我正在尝试使用 openssl 用 c 编写 Apple 推送通知服务器提供程序(APNS 提供程序)。

到目前为止,我已经能够通过 ssl_write 成功发送通知,但有时可能会发生我发送的消息被拒绝的情况(无论出于何种原因,错误的 token 等......)。当发生这种情况并且我尝试 ssl_write 时,我会写入 -1 个字节,然后 APNS 应该发回错误消息并关闭连接。

该错误以二进制形式出现,由 6 个字节组成,如 Apple 文档中所述:Apple Documenatation

const int written= SSL_write(this->_ssl, buffer, bufferSize);   

int want =SSL_want(this->_ssl);

if(want == 2 ){ //2 means that ssl wants to read
char bufferRead[6];
SSL_read(this->_ssl,bufferRead,6);

//Some code that transfers bufferRead into a ASCII format

}

我想问你,如何将这个二进制 bufferRead 转换为我可以读取并存储在 char* 或 std::string 中的内容。转换后的输出应类似于“881234”...
我很感激我能得到的任何帮助。

Eran 编辑解决方案:

   unsigned char command = bufferRead[0]; 
unsigned char status = bufferRead[1];
unsigned char c2 = bufferRead[2];
unsigned char c3 = bufferRead[3];
unsigned char c4 = bufferRead[4];
unsigned char c5 = bufferRead[5];

int comInt = command;
int statusInt = status;

int id = (c5 << 24) +
(c4 << 16) +
(c3 << 8) +
(c2);

最佳答案

您应该将缓冲区解析为 3 个参数。自从我用 c 编程以来已经有一段时间了,但我认为你需要这样的东西:

char command = bufferRead[0]; // should contain 8
char status = bufferRead[1]; // the type of the error - the most common being 8 (InvalidToken)
int id = (bufferRead[2] << 24) +
(bufferRead[3] << 16) +
(bufferRead[4] << 8) +
(bufferRead[5]);

关于c - openssl ssl_read 将读取的二进制数据发送到 char*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20524179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com