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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:48:58+00:00 2026-05-20T00:48:58+00:00

I have a weird issue trying to use AsyncSocket. In my app I actually

  • 0

I have a weird issue trying to use AsyncSocket. In my app I actually need synchronous sockets because I handle all communication in background myself on a different level. So I try to write wrapper around AsyncSocket class SyncSocket. Here is code:

// SyncSocket.h
#import <Foundation/Foundation.h>
#import "AsyncSocket.h"

@interface SyncSocket : NSObject <AsyncSocketDelegate> {
    AsyncSocket* _asyncSocket;
    NSTimeInterval _connectTimeout;
    NSTimeInterval _readTimeout;
    NSTimeInterval _writeTimeout;
    BOOL _waiting;
    BOOL _nsLog;
}

@property (nonatomic, assign) BOOL waiting;
@property (nonatomic, assign) BOOL nsLog;

- (id) initWithConnectTimeout: (NSTimeInterval) connectTimeout readTimeout: (NSTimeInterval) readTimeout writeTimeout: (NSTimeInterval) writeTimeout;

- (BOOL) connectToHost: (NSString*) host onPort: (UInt16) port;

@end

// SyncSocket.m
#import "SyncSocket.h"

@implementation SyncSocket
@synthesize waiting = _waiting;
@synthesize nsLog = _nsLog;

//
// Constructor/destructor
//

- (id) initWithConnectTimeout: (NSTimeInterval) connectTimeout readTimeout: (NSTimeInterval) readTimeout writeTimeout: (NSTimeInterval) writeTimeout {
    [super init];
    if (self == nil)
        return self;

    _connectTimeout = connectTimeout;
    _readTimeout = readTimeout;
    _writeTimeout = writeTimeout;
    _waiting = NO;

    _asyncSocket = [[AsyncSocket alloc] initWithDelegate: self];

    return self;
}

- (void) dealloc {
    [_asyncSocket release];
    [super dealloc];
}

- (BOOL) connectToHost: (NSString*) host onPort: (UInt16) port {
    if (self.nsLog)
        NSLog (@"connectToHost: %@ onPort: %d", host, port);

    _waiting = YES;
    NSError* err = nil;
    if (![_asyncSocket connectToHost: host
                      onPort: port
                     withTimeout: _connectTimeout
                   error: &err])
        return NO;

    while (self.waiting && [_asyncSocket isConnected] == NO) {
        [NSThread sleepForTimeInterval: 0.01];
    }


    return [_asyncSocket isConnected];
}

- (void) writeData: (NSData*) data {
    _waiting = YES;
    [_asyncSocket writeData: data withTimeout: _writeTimeout tag: 0];

    while (self.waiting) {
        [NSThread sleepForTimeInterval: 0.01];
    }
}


//
// AsyncSocket delegates
// 
- (BOOL)onSocketWillConnect:(AsyncSocket *)sock {
    if (self.nsLog)
        NSLog (@"onSocketWillConnect:");
    return YES;
}

- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag {
    if (self.nsLog)
        NSLog (@"didWriteDataWithTag: %d", tag);
    _waiting = NO;
}

- (void) onSocket: (AsyncSocket*) sock willDisconnectWithError: (NSError*) err {
    if (self.nsLog)
        NSLog (@"willDisconnectWithError: %d", [err code]);

    _waiting = NO;
}

- (void) onSocket: (AsyncSocket*) sock didConnectToHost: (NSString*) host port: (UInt16) port {
    if (self.nsLog)
        NSLog (@"didConnectToHost: %@ port: %d", host, port);

    _waiting = NO;
}

@end

The problem is: AsyncSocket connects to the host just fine, but my delegate didConnetToHost is never called. And if I debug AsyncSocket code I can see that even though delegate is set respondsToSelector fails. Looks like there is some strange memory violation or something but I have no idea where did I do mistake.

Any ideas? Is there a good example on how to wrap AsyncSocket to be synchronous socket?

PS: Just to be clear: XCode 3.2.5, iPhone Simulator 4.2.

Update: The reason I’m trying to wrap it with SyncSocket is because I wanted to hide all socket delegate stuff from business logic of application. I need to use socket connection in many different places and I don’t want to implement low level socket functions (like detecting state of the socket, receiving header of the message then body etc) in many different places. I want to have simple interface where I can open/send/receive and don’t worry about low level implementation. I do understand that network connectivity should be run in separate thread and that’s fine – making one background worker in the main thread is not a problem.

So I took InterfaceTest sample from AsyncSocket repository as a starting point. Removed DNS related stuff from there. Works fine. didConnect get called, everything is shiny.

I added then to the project my SyncSocket implementation to see what’s wrong with it. Project can be downloaded here: http://dl.dropbox.com/u/6402890/NetworkTest.zip – may be somebody can tell me what I’m doing wrong – I got a feeling that I’m not using threads properly in SyncSocket.

If I use in waiting loop both asyncSocket is Connected and my internal self.waiting – it gets out of that loop and didConnect is called (but AFTER the loop is finished). If I try to use only my self.waiting which is set in didConnect – didConnect never called and application hangs.

Anybody can tell me what’s wrong?

  • 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-20T00:48:58+00:00Added an answer on May 20, 2026 at 12:48 am

    Found a problem. Looks like I had to change waiting loop from using:

    [[NSThread sleepForTimeInterval: 0.01]
    

    To using RunLoop:

    [[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.01]];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a weird issue that only seems to be affecting IE 7. The
We have a semi-weird issue for our site. Consistently, from 1 anonymous* user the
I have a weird date rounding problem that hopefully someone can solve. My client
I have a weird error in my C++ classes at the moment. I have
We have a weird intermittent problem with saving from Word 2007 to our SharePoint
i have a weird problem. i would like to delete an assembly(plugin.dll on harddisk)
I have a weird problem with images in visual web developer, I cant change
I have a very weird problem: sometimes when I call nHibernate update to an
I have some extremely weird behavior that seems to result in silent exceptions. How
I have seen some very weird for loops when reading other people's code. I

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.