如何使用GCD使用CFStreams

时间:2013-07-02 09:03:52

标签: ios sockets

我是ios的新手..

我想使用Socket Connection连接到服务器,我按照给出的教程 here

这很好用,但是我在主UI空间中做了所有事情,但是视图被冻结直到我从服务器得到响应...所以我用Google搜索了一下,在ios中找到了GCD的概念。所以在我的按钮上单击我调用以下代码...

    [SVProgressHUD showWithStatus:@"Processing.."];
    [self.view setUserInteractionEnabled:NO];


  qt = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(qt, ^{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)ip, 54000, &readStream, &writeStream);

inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];

[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

[inputStream open];
[outputStream open];

NSData *data = [[NSData alloc] initWithData:[pinno dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
[outputStream close];
    });

我正在使用函数

处理事件
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {

    NSLog(@"StreamEvent %i",streamEvent);
    switch (streamEvent) {

                            dispatch_async(dispatch_get_main_queue(), ^{[SVProgressHUD dismiss];
            });

        case NSStreamEventOpenCompleted:
            NSLog(@"Stream opened");
            break;

        case NSStreamEventHasBytesAvailable:
                 NSLog(@"Stream has byte Avaliable");

            if (theStream==inputStream) {

                while ([inputStream hasBytesAvailable]) {
                    uint8_t buffer[1024];
                    int len;
                    len = [inputStream read:buffer maxLength:sizeof(buffer)];
                    if(len>0){
                        NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
                        [self.view setUserInteractionEnabled:YES];

                        NSLog(@"output %@",output);


                    }
                }
            }
            break;

        case NSStreamEventErrorOccurred:
            [self.view setUserInteractionEnabled:YES];

            NSLog(@"Can not connect to the host!");
            break;

        case NSStreamEventEndEncountered:
            [self.view setUserInteractionEnabled:YES];

            break;

        default:
            [self.view setUserInteractionEnabled:YES];

            NSLog(@"Unknown event");

    }
}

使用上面的代码我得到了一个请求到服务器,服务器也发送了一个成功的响应,但我没有收到句柄事件方法中的任何数据,但我没有得到一个hasbytesavaliable事件..我的控制台日志是

2013-07-02 14:06:15.312 Multi[899:1c03] StreamEvent 1
2013-07-02 14:06:15.313 Multi[899:1c03] Stream opened
2013-07-02 14:06:15.313 Multi[899:1c03] StreamEvent 4
2013-07-02 14:06:15.313 Multi[899:1c03] Unknown event

知道我做错了什么......

Edit:Updated Code

在按钮上单击我启动计时器并调用连接方法..

counter=[NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(stream:handleEvent:) userInfo:nil repeats:NO];


        qt = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(qt, ^{[self connect];});

连接方法:

  CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)ip, 54000, &readStream, &writeStream);


    inputStream = (NSInputStream *)readStream;
    outputStream = (NSOutputStream *)writeStream;

    NSRunLoop *loop = [NSRunLoop currentRunLoop];

    [inputStream setDelegate:self];
    [outputStream setDelegate:self];

    [inputStream scheduleInRunLoop:loop forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:loop forMode:NSDefaultRunLoopMode];

    [inputStream open];
    [outputStream open];

    NSData *data = [[NSData alloc] initWithData:[pinno dataUsingEncoding:NSASCIIStringEncoding]];
    [outputStream write:[data bytes] maxLength:[data length]];
    [outputStream close];

    [loop run];

流处理事件

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
    Username.text = @"";
    Password.text = @"";
    [self disconnect];
    if (counter!=nil){
        [counter invalidate];
        counter=nil;
    }

    NSLog(@"StreamEvent %i",streamEvent);
    switch (streamEvent) {

        case NSStreamEventOpenCompleted:
            NSLog(@"Stream opened");
            break;

        case NSStreamEventHasBytesAvailable:
            if (theStream==inputStream) {

                while ([inputStream hasBytesAvailable]) {
                    uint8_t buffer[1024];
                    int len;
                    len = [inputStream read:buffer maxLength:sizeof(buffer)];
                    if(len>0){
                        NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];

                        NSLog(@"output %@",output);
                        [SVProgressHUD dismiss];
                        main = [[Mainmenu alloc]
                                initWithNibName:@"Mainmenu"
                                bundle:nil];
                        //                        [self.view addSubview:main.view];
                        [self presentViewController:main animated:NO completion:NO];
                    }
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{[SVProgressHUD dismiss];
            });

            break;

        case NSStreamEventErrorOccurred:

            NSLog(@"Can not connect to the host!");
            dispatch_async(dispatch_get_main_queue(), ^{[self dismiss:@"Cannot connect to host!"];
            });

            break;

        case NSStreamEventEndEncountered:
            dispatch_async(dispatch_get_main_queue(), ^{[self dismiss:@"Connection Error"];
            });

            break;

        default:
            NSLog(@"Unknown event");
            dispatch_async(dispatch_get_main_queue(), ^{[self dismiss:@"Timeout Expired"];
            });

    }
}

断开方法..

-(void) disconnect
{
    [inputStream close];
    [inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream close];
    [outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [inputStream setDelegate:nil];
    inputStream = nil;
    [outputStream setDelegate:nil];
    outputStream = nil;

}

1 个答案:

答案 0 :(得分:4)

如果您在单独的线程上运行它,那么您必须处理Run Loop。你必须打电话给

  

[[NSRunLoop currentRunLoop] run];

在合适的时间。

如下所示更改您的这部分代码,它肯定会起作用:

inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;

NSRunLoop *loop = [NSRunLoop currentRunLoop];

[inputStream setDelegate:self];
[outputStream setDelegate:self];

[inputStream scheduleInRunLoop:loop forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:loop forMode:NSDefaultRunLoopMode];

[inputStream open];
[outputStream open];

[loop run];