我正在尝试将我的 iOS 应用程序连接到 adhoc 网络上的打印机,在向打印机发送任何内容之前,我需要检查连接是否打开且有效,我该如何实现?目前我正在执行以下 CFReadStreamRef readStream; CFWriteStreamRef 写流;
readStream = NULL;
writeStream = NULL;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)IPADDRESS, NUM_PORT,NULL ,&writeStream );
if(writeStream)
{
//inputStream = (__bridge NSInputStream *)readStream;
oStream = (__bridge NSOutputStream *)writeStream;
//[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
//[iStream setDelegate:delegate];
[oStream setDelegate:delegate];
//[iStream open];
[oStream open];
}
和
int bytesWritten;
int bytesTotal = [data length];
bytesWritten = [oStream write:[data bytes] maxLength: bytesTotal];
NSLog(@"Bytes Written To Printer : %d",bytesWritten);
但它总是会进入并且 writeStream 永远不会为空,所以我无法阻止它尝试将数据推送到套接字。
〜编辑 - 也许我最好改写我的问题,我怎样才能阻止它挂起 30 多秒,直到它意识到没有连接?