Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8077359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:27:27+00:00 2026-06-05T15:27:27+00:00

for some reason when I try to call CocoaAsyncSocket’s onSocket:didReadData:withTag method, it’s failing and

  • 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:27:28+00:00Added an answer on June 5, 2026 at 3:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, I try to call AdjustWindowRect, or GetClientRect, and the parameters I
I'm getting data from an array. For some reason the array has key values
For some reason, onreadystatechange call back function is not being called in asynchronous mode.
For some reason the following code causes a SIGSEGV when I try to call
For some reason if I try to get the actual size of mystruct I
I'm getting undefined for some reason when I try to return the html via
This is my first applet project and for some reason when I try to
I'm hoping you can give me some pointers... For some reason when I try
For some reason every time I try to count the number of rows in
for some reason then i duplicate movie clip and try to put it in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.