2012-06-06 19 views
0

我已經創建了一個使用CocoaAsyncSocket的TCP套接字連接,並且每當我嘗試執行didReadData時,我都會返回空格。當我設置斷點並嘗試調試時,我發現「msg」的值爲@「」。使用CocoaAsyncSocket讀取數據返回空格

這是我的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:插座didReadData:數據withTag:方法:

- (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:數據withTag:方法不被調用。任何幫助,將不勝感激。謝謝!

回答

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是通過了可可一個非常重要的模式(點觸摸),確保你得到它的權利。

+0

什麼是DDLogError?你自己的日誌功能? –

+0

哦,不,它來自[Lumberjack](https://github.com/robbiehanson/CocoaLumberjack),一組有用的工具。 – vikingosegundo

+1

我還沒有徹底地通過StackOverflow的指導,但我認爲最好不要在示例代碼中使用第三方的東西。 NSLog()會保持簡單,不太可能混淆新手。 –