0

for some reason when I try to call CocoaAsyncSocket's onSocket:didReadData:withTag method, it's failing and not showing data from the read.

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

And here are my methods from the CocoaAsyncSocket Library:

- (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");
}

When I run my app in the simulator, this is what my output log spits out:

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 is called, and NSLog spits out "connected".
  • onSocket:didReadData:withTag is called, and NSLog spits out "RX length: 8".
  • also in onSocket:didReadData:withTag, NSLog spits out "Fail".

Correct me if I'm wrong, but it shouldn't "msg" contain the value of the data read from the server? It looks like the Data Read knows that it's expecting a data length of 8, but the data doesn't seem to get stored into the string. I'm very new to iOS and especially socket programming, so this is all new to me. Any help on this subject would be greatly appreciated.

Thanks!

Skizz
  • 1,211
  • 5
  • 17
  • 28
  • I dont know, what your server is supposed to do, but dont you need to send something to it? – vikingosegundo Jun 08 '12 at 17:43
  • The server (written by somebody else) is waiting for connections. As soon as a connection is made, a value (I believe it's a long type) is sent to the client. I'm trying to get the value of the long type and put it into a string. – Skizz Jun 08 '12 at 17:52
  • This doesn't make a whole lot of sense to me: NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length])]; You want the subdata of an NSData object that is the entire length of the original data? I think you can omit that line... – Scott Berrevoets Jun 08 '12 at 17:54
  • are you sure, the data is UTF-8 encoded? – vikingosegundo Jun 08 '12 at 18:01
  • I'm not positive that the data is UTF-8 encoded. Another person developed the server side application and he is not available today. If the data is not UTF-8 encoded, then I guess it would fail, right? If that's the case, I'll have to put this on hold for a few days until I know. Thanks again for the speedy replies! – Skizz Jun 08 '12 at 18:05

1 Answers1

1

I believe, that the server is delivering data, that isnt UTF-8 encoded.
please try

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

and remove this line:NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length])];

If this doesn't help, here are some more encodings to try:

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

with NSISOLatin1StringEncoding also being quite common.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • It looks like I'm getting closer to solving this! I made changes in my code to reflect your code change suggestion and now I'm getting "2012-06-08 14:05:38.215 tekMatrix[3072:f803] RX: " with an upside down question mark after "RX: ". Thanks for all the help. You're a very high quality SO user :) – Skizz Jun 08 '12 at 18:07
  • I removed "NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length])];" and it caused my application to completely crash. I put that line back in so that it doesn't crash. – Skizz Jun 08 '12 at 18:09
  • Yes, I replaced "NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length])];" with "NSString *msg = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];" and it caused the app to crash. Right now I have kept both lines of code to keep the app from crashing. Why is it important to eliminate the strData line of code? – Skizz Jun 08 '12 at 18:13
  • You are actually creating a copy of the first data object. this should be unnecessary. – vikingosegundo Jun 08 '12 at 18:16
  • Strange, it seems to be working properly now. I was tinkering around with removing/adding code. maybe I typo'd the first time or something. I'm making progress, but now the app is "hanging" after all the methods are called. I created a new question on it, because I'm baffled why it's happening. Thanks for all the help... again :D – Skizz Jun 08 '12 at 18:23