从我的 PC 应用程序中,我将套接字数据发送到我的 iPhone。当数据到达时,它被存储为有符号值而不是无符号 (uint8)。所以我无法得到超过 127 的任何值。
当我发送 0xFF 时,它默认返回 0x3f。
CFReadStreamRead 是否仅适用于 ASCII 字符?如果是这样,什么是替代品?
我的代码:
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
CFIndex bytes;
UInt8 buffer[128];
UInt8 recv_len = 0;
UInt8 send_len = 0;
//
//Get the native socket that was used
CFSocketNativeHandle sock = *(CFSocketNativeHandle*)data;
//
//Create the read and write streams for this socket
CFStreamCreatePairWithSocket(kCFAllocatorDefault, sock, &readStream, &writeStream);
//
//Check for errors
if (!readStream || !writeStream)
{
close(sock);
fprintf(stderr, "CFStreamCreatePairWithSocket failed.");
return;
}
//
//Open the streams
CFReadStreamOpen(readStream);
CFWriteStreamOpen(writeStream);
//
//Read the command bytes
short command = 0;
memset(buffer, 0, sizeof(buffer));
bytes = CFReadStreamRead(readStream, buffer, 12);