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

  • Home
  • SEARCH
  • 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 6889211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:07:39+00:00 2026-05-27T06:07:39+00:00

I am trying to simply send and recieve a message using GCDAsyncSocket and cannot

  • 0

I am trying to simply send and recieve a message using GCDAsyncSocket and cannot get it to work.

I am successfully establishing the connection and writing messages, but when it comes to reading my delegate is never called.

I am im using ios5 and this setup:

Client:

-(void) connectToHost:(HostAddress*)host{

    NSLog(@"Trying to connect to host %@", host.hostname);

    if (asyncSocket == nil)
    {
        asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

        NSError *err = nil;
        if ([asyncSocket connectToHost:host.hostname onPort:host.port error:&err])
        {
            NSLog(@"Connected to %@", host.hostname);

            NSString *welcomMessage = @"Hello from the client\r\n";
            [asyncSocket writeData:[welcomMessage dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:1];

            [asyncSocket readDataWithTimeout:-1 tag:0];
        }else
            NSLog(@"%@", err);
    }

}

Delegate didReadData method is not called

-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{

    NSLog(@"MESSAGE: %@", [NSString stringWithUTF8String:[data bytes]]);

}

Server

-(void)viewDidLoad{

    asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

    connectedSockets = [[NSMutableArray alloc] init];

    NSError *err = nil;
    if ([asyncSocket acceptOnPort:0 error:&err]){

        UInt16 port = [asyncSocket localPort];

        //...bojour stuff
    }
    else{
        NSLog(@"Error in acceptOnPort:error: -> %@", err);
    }

}

Write a message to client and wait for response on successful socket connection

- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
    NSLog(@"Accepted new socket from %@:%hu", [newSocket connectedHost], [newSocket connectedPort]);

    // The newSocket automatically inherits its delegate & delegateQueue from its parent.

    [connectedSockets addObject:newSocket];

    NSString *welcomMessage = @"Hello from the server\r\n";
    [asyncSocket writeData:[welcomMessage dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:1];

    [asyncSocket readDataWithTimeout:-1 tag:0];

}

And this is not called ever…

-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
    NSLog(@"New message from client... ");
}
  • 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-05-27T06:07:40+00:00Added an answer on May 27, 2026 at 6:07 am

    Ok, found the answer.

    The problem was that I was writing and reading on my own socket end instead of the connected socket.

    Fix: (changed asyncSocket to newSocket)

    - (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket
    {
        NSLog(@"Accepted new socket from %@:%hu", [newSocket connectedHost], [newSocket connectedPort]);
    
        // The newSocket automatically inherits its delegate & delegateQueue from its parent.
    
        [connectedSockets addObject:newSocket];
    
        NSString *welcomMessage = @"Hello from the server\r\n";
        [newSocket writeData:[welcomMessage dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:1];
    
        [newSocket readDataWithTimeout:-1 tag:0];
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to send messages generated by Google Protocol Buffer code via a simple
I'm simply trying to crop a JPEG image (no scaling) using PHP. Here is
I am simply trying to draw a rectangle inside of a panel using flex4.
I'm trying to simply test my RESTful API with cURL. Using the following invocation:
I'm trying to get a simple example working with GCDAsyncSocket, and am discovering that
I'm trying to get a simple send and receive UDP program working, but I'm
I am trying to send data using the AsyncUDPSocket class. And I can send
I've got a problem trying to send a request using QNetworkAccessManager from a QObject
Hi I am trying to send a simple HTTP message from Flex to C#
I am trying to send audio file from one computer to other using socket

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.