CFRunLoopRun()问题

时间:2011-03-11 05:16:05

标签: iphone

我在CFRunLoopRun()

上遇到了问题

这是代码 点击按钮后,它停止在CFRunLoopRun()方法中一段时间​​, 任何人都可以帮助我做下一个............

我的主要问题是我必须使用BOCA打印机连接使用IP地址和端口号,我已完成向BOCA发送消息,但我的问题是如何获得返回响应,或者我怎么知道我连接到打印机,当我的系统和BOCA打印机连接一个NETWork连接时,应该发送打印消息........

- (IBAction为)ConnectButtonPressed

{if(isPrinterConnected == FALSE)

{     char print [] =“print”;     / *构建我们的套接字上下文;这把笑话与插座联系起来* /     CFSocketContext CTX = {0,print,NULL,NULL,NULL};

/* Create the server socket as a TCP IPv4 socket and set a callback */
/* for calls to the socket's lower-level connect() function */
TCPClient = CFSocketCreate(NULL, PF_INET, SOCK_STREAM, IPPROTO_TCP,
                           kCFSocketConnectCallBack, (CFSocketCallBack)ConnectCallBack, &CTX);
if (TCPClient == NULL)
{
}
else{

    /* Set the port and address we want to listen on */
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_len = sizeof(addr);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    //addr.
    //addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_addr.s_addr = htonl((((((10 << 8) | 0) << 8) | 0) << 8) | 204);

    CFDataRef connectAddr = CFDataCreate(NULL, (unsigned char *)&addr, sizeof(addr));

    CFSocketConnectToAddress(TCPClient, connectAddr, -1);
    CFRunLoopSourceRef sourceRef = CFSocketCreateRunLoopSource(kCFAllocatorDefault, TCPClient, 0);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), sourceRef, kCFRunLoopCommonModes);
    CFRelease(sourceRef);
    CFRunLoopRun();
    NSLog(@"next line ");

//isPrinterConnected = TRUE;


//[connect setTitle: @"Disconnect" forState: UIControlStateNormal];
}

}else 

{     关闭(袜子);     isPrinterConnected = FALSE;     [myButton setTitle:@“Connect”forState:UIControlStateNormal];   }

}

1 个答案:

答案 0 :(得分:2)

使用CFRunLoopRun,您可以告诉iOS停止执行代码并运行在runloop中安排的所有内容。这是一个阻塞调用,只有在runloop中执行的代码调用CFRunLoopStop时才会返回。