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

时间:2013-12-11 16:10:17

标签: c binary openssl apple-push-notifications

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

到目前为止,我已经能够通过ssl_write成功发送通知,但有时会发生,我发送的消息被拒绝(无论出于何种原因,坏令牌等等)。当发生这种情况并且我尝试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的EDIT解决方案:

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

1 个答案:

答案 0 :(得分:0)

您应该将缓冲区解析为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]);