使用CocoaAsyncSocket读取数据会返回空白

时间:2012-06-06 19:33:32

标签: objective-c ios cocoa-touch cocoaasyncsocket

我使用CocoaAsyncSocket创建了一个TCP套接字连接,每当我尝试执行didReadData时,我都会回到空白状态。当我设置断点并尝试调试时,我发现“msg”的值为@“”。

这就是我的appDelegate.m的样子:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSData *data = nil;
    // Initialize socket object and make it a delegate.  Then call the delegate methods.
    socket = [[AsyncSocket alloc] initWithDelegate:self];
    [self connect];
    [self onSocket:socket didConnectToHost:@"9.5.8.6" port:11005];
    [self onSocket:socket didReadData:data withTag:1]; // tags COULD be #defined *******

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[tekMatrixViewController alloc]  initWithNibName:@"tekMatrixViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

这是我的onSocket:socket didReadData:data withTag:method:

- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length])];
    NSString *msg = [[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding];
    if(msg)
    {
        NSLog(@"RX:%@",msg);
        if(msg == nil)
        {
            NSLog(@"msg is all Blanks");
        }
    } 
    else 
    {
        NSLog(@"Fail");
    }    
}

注意,此方法是CocoaAsyncLibrary中的方法。我不知道我是否正确地调用了该方法,或者我是否正在传递正确的参数,或者是什么。

当我运行应用程序时,我在控制台中看到的只有:

2012-06-06 11:44:00.434 tekMatrix[1378:f803] connected
2012-06-06 11:45:14.312 tekMatrix[1378:f803] RX:

非常感谢任何和所有帮助。

谢谢!

修改

以下是我在didFinishLaunchingWithOptions方法中的内容:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    socket = [[AsyncSocket alloc] initWithDelegate:self];
    NSError *error = nil;
    if (![socket connectToHost:@"199.5.83.63" onPort:11005 error:&error]) 
    {
        NSLog(@"Error connecting: %@", error);
    }

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[tekMatrixViewController alloc] initWithNibName:@"tekMatrixViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

现在我能够看到我已连接,但我仍然没有理解onSocket:socket didReadData:data withTag:方法不被调用。任何有关这方面的帮助将非常感激。谢谢!

1 个答案:

答案 0 :(得分:3)

抱歉,我不得不说:你们所有关于代表的错误。

你没有调用你实现的方法来自己完成委托的协议 - 委托人(在这种情况下是套接字)就是这样做

所以这段代码

[self connect];
[self onSocket:socket didConnectToHost:@"9.5.8.6" port:11005];
[self onSocket:socket didReadData:data withTag:1]

根本没有任何意义。删除它。

相反应该有以下代码来连接

NSError *error = nil;
if (![socket connectToHost:@"9.5.8.6" onPort:11005 error:&error]) {
    NSLog(@"Error connecting: %@", error);
}

而且,套接字将调用socket:didConnectToHost:port:,可能看起来像

-(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
{
        [sock writeData:self.data withTimeout:-1 tag:1];

}

并且对象套接字将调用您提供的进一步委托方法实现。


但是delegation是一个非常重要的模式,通过可可(-touch),确保你做对了。