目前正在写一iOS客户端,内容是客户端向服务端发送一段命令,服务端通过某端口向客户端发送图像 我用CFSocket,CFStream写了如下代码; 但是在文件接收后,无法得知何时文件已经传送完成,也没有个完成状态神马的: kCFStreamEventEndEncountered这里也不被执行; 用了GCDAsyncSocket也是如此,
因为不想改变服务端原有的架构,所以尽量不通过协议来解决; iOS在文件传输完毕之后有EOF之类的标识么?
int setupSocket() {
_receiveData = [[NSMutableData alloc] init];
_socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, TCPServerAcceptCallBack, NULL);
if (NULL == _socket) {
NSLog(@"Cannot create socket!");
return 0;
}
int optval = 1;
setsockopt(CFSocketGetNative(_socket) , SOL_SOCKET, SO_REUSEADDR, (void *)&optval, sizeof(optval));
struct sockaddr_in addr4;
memset(&addr4, 0, sizeof(addr4));
addr4.sin_len = sizeof(addr4);
addr4.sin_family = AF_INET;
addr4.sin_port = htons(12000);
addr4.sin_addr.s_addr = htonl(INADDR_ANY);
CFDataRef address = CFDataCreate(kCFAllocatorDefault, (UInt8 *)&addr4, sizeof(addr4));
if (kCFSocketSuccess != CFSocketSetAddress(_socket, address)) {
NSLog(@"Bind to address failed!");
if (_socket)
CFRelease(_socket);
_socket = NULL;
return 0;
}
CFRunLoopRef cfRunLoop = CFRunLoopGetCurrent();
CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(kCFAllocatorDefault, _socket, 0);
CFRunLoopAddSource(cfRunLoop, source, kCFRunLoopCommonModes);
CFRelease(source);
return 1;
}
void TCPServerAcceptCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) {
if (kCFSocketAcceptCallBack == type) {
// 本地套接字句柄
CFSocketNativeHandle nativeSocketHandle = *(CFSocketNativeHandle *)data;
uint8_t name[SOCK_MAXADDRLEN];
socklen_t nameLen = sizeof(name);
if (0 != getpeername(nativeSocketHandle, (struct sockaddr *)name, &nameLen)) {
NSLog(@"error");
exit(1);
}
NSLog(@"%s connected.", inet_ntoa( ((struct sockaddr_in *)name)->sin_addr));
CFReadStreamRef iStream;
// 创建一个可读写的socket连接
CFStreamCreatePairWithSocket(kCFAllocatorDefault, nativeSocketHandle, &iStream, NULL);
if (iStream) {
CFStreamClientContext streamContext = {0, NULL, NULL, NULL};
if (!CFReadStreamSetClient(iStream,
kCFStreamEventHasBytesAvailable,
readStream, // 回调函数,当有可读的数据时调用
&streamContext)){
exit(1);
}
CFReadStreamScheduleWithRunLoop(iStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
CFReadStreamOpen(iStream);
} else {
close(nativeSocketHandle);
}
}
}
// 读取数据 void readStream(CFReadStreamRef stream, CFStreamEventType eventType, void *clientCallBackInfo) {
printf("新一轮开始了----------------------");
switch (eventType) {
case kCFStreamEventEndEncountered:
NSLog(@"完成");
break;
case kCFStreamEventHasBytesAvailable:
{
UInt8 buff[1024];
CFReadStreamRead(stream, buff, 1024);
for (int i = 0; i <1024; i++) {
printf("index:%d-%dn",i,buff[i]);
}
[_receiveData appendData:[NSData dataWithBytes:buff length:1024]];
}
default:
break;
}
}
1楼 @tinyfool 服务端是tcp的,目前已经是一套在用的产品了,已经有android的客户端,原有的架构不可变。
我找到原因了,tiny大又会批评我没好好看文档了。T_T 这里要指定监听的状态类型:
if (!CFReadStreamSetClient(iStream, kCFStreamEventHasBytesAvailable|kCFStreamEventEndEncountered, readStream, // 回调函数,当有可读的数据时调用 &streamContext)){ exit(1); }
kCFStreamEventHasBytesAvailable & kCFStreamEventEndEncountered