我正在尝试将我的iOS应用程序连接到adhoc网络上的打印机,我需要在向打印机发送任何内容之前检查连接是否已打开且有效,我该如何实现?目前我正在做以下事情 CFReadStreamRef readStream; CFWriteStreamRef writeStream;
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永远不会为null所以我无法阻止它尝试将数据推送到套接字。
〜编辑 - 也许我最好解释一下我的问题,我怎么能阻止它挂30秒以上直到它意识到没有连接?
答案 0 :(得分:1)
在向打印机发送任何内容之前,我需要检查连接是否已打开且有效,我该如何实现?
通过向打印机发送内容。检测TCP中的连接问题的唯一方法是使用它进行一些I / O.