【发布时间】:2013-12-29 17:22:01
【问题描述】:
我正在尝试使用 openssl 在 c 中编写 Apple Push Notification Server 提供程序(APNS 提供程序)。
到目前为止,我已经能够通过 ssl_write 成功发送通知,但有时可能会发生我发送的消息被拒绝(无论出于何种原因,令牌错误等)。当发生这种情况并且我尝试 ssl_write 时,我会写入 -1 个字节,然后 APNS 应该发回错误消息并关闭连接。
错误以二进制形式出现,由 6 个字节组成,如苹果文档中所述: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);
【问题讨论】:
标签: c binary openssl apple-push-notifications