由于某些原因,当我尝试调用CocoaAsyncSocket的onSocket:didReadData:withTag方法时,它失败了,并且不显示读取的数据。

appDelegate:

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

    [socket readDataWithTimeout:10 tag:1];

    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;
}


这是我来自CocoaAsyncSocket库的方法:

- (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];
    NSLog(@"RX length: %d", [data length]);
    if(msg)
    {
        NSLog(@"RX:%@",msg);
    }
    else
    {
        NSLog(@"Fail");
    }
}

- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
{
    NSLog(@"error - disconnecting");
    //start reconnecting procedure here...
}

- (void)onSocketDidDisconnect:(AsyncSocket *)sock
{
    NSLog(@"disconnected");
}

- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
    NSLog(@"connected");
}


当我在模拟器中运行应用程序时,这是我的输出日志显示的内容:

2012-06-08 13:17:30.808 tekMatrix[2793:f803] connected
2012-06-08 13:17:30.815 tekMatrix[2793:f803] RX length: 8
2012-06-08 13:17:30.816 tekMatrix[2793:f803] Fail



onSocket:didConnectToHost:port被调用,并且NSLog吐出“已连接”。
调用onSocket:didReadData:withTag,NSLog弹出“ RX length:8”。
同样在onSocket:didReadData:withTag中,NSLog吐出“失败”。


如果我错了,请纠正我,但是“ msg”不应该包含从服务器读取的数据的值吗?看起来Data Read知道期望的数据长度为8,但是数据似乎没有存储到字符串中。我对iOS尤其是套接字编程非常陌生,因此这对我来说是全新的。在这个问题上的任何帮助将不胜感激。

谢谢!

最佳答案

我相信,该服务器正在传递不是UTF-8编码的数据。
请试试

NSString *msg = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

并删除以下行:NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length])];
如果这样做没有帮助,请here are some more encodings尝试:
enum {
   NSASCIIStringEncoding = 1,
   NSNEXTSTEPStringEncoding = 2,
   NSJapaneseEUCStringEncoding = 3,
   NSUTF8StringEncoding = 4,
   NSISOLatin1StringEncoding = 5,
   NSSymbolStringEncoding = 6,
   NSNonLossyASCIIStringEncoding = 7,
   NSShiftJISStringEncoding = 8,
   NSISOLatin2StringEncoding = 9,
   NSUnicodeStringEncoding = 10,
   NSWindowsCP1251StringEncoding = 11,
   NSWindowsCP1252StringEncoding = 12,
   NSWindowsCP1253StringEncoding = 13,
   NSWindowsCP1254StringEncoding = 14,
   NSWindowsCP1250StringEncoding = 15,
   NSISO2022JPStringEncoding = 21,
   NSMacOSRomanStringEncoding = 30,
   NSUTF16StringEncoding = NSUnicodeStringEncoding,
   NSUTF16BigEndianStringEncoding = 0x90000100,
   NSUTF16LittleEndianStringEncoding = 0x94000100,
   NSUTF32StringEncoding = 0x8c000100,
   NSUTF32BigEndianStringEncoding = 0x98000100,
   NSUTF32LittleEndianStringEncoding = 0x9c000100,
   NSProprietaryStringEncoding = 65536
};

NSISOLatin1StringEncoding也很常见。

09-10 23:23