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 8879015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:51:11+00:00 2026-06-14T19:51:11+00:00

I am using CocoaAsyncSocket , I need to make a function which sends a

  • 0

I am using CocoaAsyncSocket , I need to make a function which sends a message to the server and wait until the server replies, in delegate methods it does receive server response but I need the function that is sending the message wait for the server to reply and return response.

- (NSString *)sendMessage:(NSString *)message{
    NSError *err = nil;
    if (![socket connectToHost:@"192.168.1.20" onPort:52523 error:&err]) // Asynchronous!
    {
        NSLog(@"Error is : %@", err);
    }

   [socket readDataWithTimeout:-1 tag:1];

    NSData* data =[message dataUsingEncoding:NSUTF8StringEncoding];
    [socket writeData:data withTimeout:-1 tag:1];

    NSString *xmlString = [[NSString alloc] init];
    // Here I need the function wait and receive the response

    return xmlString;
}
  • 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-14T19:51:13+00:00Added an answer on June 14, 2026 at 7:51 pm

    If you need to send something synchronously why not build a request and use NSURLConnection api like here:

    NSData* data = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];   
    

    This will block until you get a response.

    If you want to keep using the async socket approach but forcibly make it a synchronous call you can do this by adding the following methods:

    @property (nonatomic, assign) BOOL com_private_condition;
    @property (nonatomic, assign) NSThread* com_private_theWaitingThread;
    

    …

    @synthesize com_private_condition;
    @synthesize com_private_theWaitingThread;
    

    …

        - (BOOL)waitForConditionWithTimeout:(NSTimeInterval)aTimeout
        {
            self.com_private_condition = NO;
            self.com_private_theWaitingThread = [NSThread currentThread];
            NSDate* theStartDate = [NSDate date];
            NSDate* theEndDate = [NSDate dateWithTimeIntervalSinceNow:aTimeout];
            do 
            {
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode 
                                     beforeDate:theEndDate];
            NSTimeInterval theElapsedTime = -[theStartDate timeIntervalSinceNow];
            if (theElapsedTime >= aTimeout)
            {
                return NO;
            }
            if (self.com_private_condition)
            {
                return YES;
            }
        } while (YES);
    }
    
    - (void)signalCondition
    {
        [self performSelector:@selector(com_private_signalCondition:) 
                     onThread:self.com_private_theWaitingThread 
                   withObject:nil waitUntilDone:NO];
    }
    
    - (void)com_private_signalCondition:(id)aParam
    {
        self.com_private_condition = YES;    
    }
    

    Now make your method like this

    - (NSString *)sendMessage:(NSString *)message{
        NSError *err = nil;
        if (![socket connectToHost:@"192.168.1.20" onPort:52523 error:&err]) // Asynchronous!
        {
            NSLog(@"Error is : %@", err);
        }
    
       [socket readDataWithTimeout:-1 tag:1];
    
        NSData* data =[message dataUsingEncoding:NSUTF8StringEncoding];
        [socket writeData:data withTimeout:-1 tag:1];
    
        //make xmlString a variable on your class and set it in your async socket callback when you get the data. once the wait is over just return it.
        //NSString *xmlString = [[NSString alloc] init];
        // Here I need the function wait and receive the response
    
        //call read and then wait
        [socket readDataWithTimeout:-1 tag:1];
        [self waitForConditionWithTimeout:9999.0]; //or some other wait timeout of your choosing
        //this will block until [self signalCondition] is called by your socket callbacks.
    
        return self.xmlString;
    }
    

    Now in your CocoaAsyncSocket callback socket:didReadData:withTag: and socket:didDisconnectWithError: make sure you call

    [self signalCondition];
    

    Once you call that the waiting method will continue, and you’ve just make an asynchronous call synchronous.

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

Sidebar

Related Questions

Using Delphi 2010. I am looking for (possibly) a function or procedure which can
I am currently building an App using CocoaAsyncSocket. I connect to a TCP server
Using SQL Server 2008 R2 we are looking for a way to select the
Using Microsoft SQL Server 2005, is there any way to see when a table
I'm trying to implement this (Configuring Sockets for VoIP Usage) using this (CocoaAsyncSocket). To
Using MVC2 I have an AJAX form which is posting to a bound model.
using a binary search tree I need to add to a vector all int
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
I'm using cocoaasyncsocket to send data Google Protocol Buffers (using http://code.google.com/p/metasyntactic/wiki/ProtocolBuffers ) to a
Using MVVM. I have a DataTemplate which I am using to display an expander

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.