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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:20:47+00:00 2026-05-23T10:20:47+00:00

I’m trying to do the following with a modified version of the echo server

  • 0

I’m trying to do the following with a modified version of the echo server example that comes with the cocoaasyncsocket library:

1) open a connection to a python script acting as a server
2) send data // works, but delegate doesn’t fire
3) receive data back // delegate doesn’t fire
4) disconnect // doesn’t disconnect, apparently still in my thread

Currently I open a connection in the didFinishLaunchingWithOptions delegate, and then attempt to send data in the didConnectToHost delegate. I then attempt to read data coming back from the client and then disconnect.

I am able to open a connection and send data (which the server verifies as received) but the didWriteDataWithTag delegate never fires. However, the server receive the data. The server then fires back some data, but the didReadData doesn’t fire either.

Beside the fact the read/write delegates aren’t firing, it seems the way I’m organizing my code is not right, but I’m not sure how this looks in an event-driven system as opposed to run loop (I’m a novice at event-driven stuff + networking). If I have a series of actions whose respective completions are triggered by their delegates, should the delegates be sharing some sort of messages- ie we recieved an “xxx” message, write back “yyy”? I’d prefer to have one function which manages all of this. Is there a canonical way of doing this?

IPhoneConnectTestAppDelegate.m (snippets)

- (void)localConnect {
    NSError *error = nil;
    if (![asyncSocket connectToHost:@"localhost" onPort:5000 error:&error]) {
        DDLogError(@"Error connecting: %@", error);
    }
}

- (void)disconnect {
    [asyncSocket setDelegate:nil];
    [asyncSocket disconnect];
    [asyncSocket release];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Setup our socket (GCDAsyncSocket).
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];

    [self localConnect];

    // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}


- (void)onSocket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
    NSString *output = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSLog(@"didReadData: %@", output);

}

- (void)onSocket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag {
    NSLog(@"didWriteDataWithTag");
}

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port {
    NSLog(@"socket:%p didConnectToHost:%@ port:%hu", sock, host, port);
    if(port == 5000)
    {
        NSString *msg = @"q";
        NSData *dataOut = [msg dataUsingEncoding:NSASCIIStringEncoding];
        [asyncSocket writeData:dataOut withTimeout:-1 tag:0];
        [asyncSocket readDataWithTimeout:-1 tag:0];
        [self disconnect];
    }
}

tcpserver.py

# TCP server example
import socket, time
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 5000))
server_socket.listen(5)

print "TCPServer Waiting for client on port 5000"

while 1:
    client_socket, address = server_socket.accept()
    print "I got a connection from ", address
    while 1:
        data = client_socket.recv(512)
        print "Data from client",data

        time.sleep(2)
        data = "xxx"
        print "Sending data to client",data
        client_socket.send (data)
        break;
  • 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-23T10:20:48+00:00Added an answer on May 23, 2026 at 10:20 am

    A friend of a friend figured it out for me! We were not able to get GCDAsyncSocket to work properly (connect and write, but not read). AsyncSocket however functions in all 3 respects, and all the delegates work properly.

    #import "AsyncSocket.h"
    #import "tcp_clientViewController.h"
    
    @implementation tcp_clientViewController
    
    @synthesize socket;
    
    
    
    -(IBAction)connect:(id)sender { 
        NSLog(@"(IBAction)connect");
        NSError *error = nil;
        if (![socket connectToHost:@"localhost" onPort:5000 error:&error]){
            NSLog(@"Error connecting: %@", error);
        }
    }
    
    -(IBAction)send:(id)sender {
        NSLog(@"(IBAction)send");
    
    
        char bytes[] = "abcd\r\n";
        NSData* data = [[NSData alloc] initWithBytes:bytes length:sizeof(bytes)];
    
        //NSString *msg = @"xxxxx\r\n";
        //NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];
    
    
        [socket writeData:data withTimeout:-1 tag:0];
        //NSData *data = [asyncSocket readDataWithTimeout:-1 tag:0];
        [data release];
    
        [socket readDataToData:[AsyncSocket LFData] withTimeout:-1 tag:0];
    }
    
    - (void)viewDidLoad {
        // initialize socket
        socket = [[AsyncSocket alloc] initWithDelegate:self];
    }
    
    #pragma mark AsyncSocket Delegate Methods
    -(void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag {
        NSLog(@"socket:%p didWriteDataWithTag:%@", sock, tag);
    }
    
    - (void)socket:(AsyncSocket *)sock didWritePartialDataOfLength:(NSUInteger)partialLength tag:(long)tag {
        NSLog(@"socket:%p didWritePartialDataOfLength:%@ tag:%@", sock, partialLength, tag);
    }
    
    - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
    {
        NSLog(@"socket:%p didConnectToHost:%@ port:%hu", sock, host, port);
    }
    
    - (void)socketDidSecure:(AsyncSocket *)sock
    {
        NSLog(@"socket:%p socketDidSecure", sock);
    }
    
    - (void)socketDidDisconnect:(AsyncSocket *)sock withError:(NSError *)err
    {
        NSLog(@"socket:%p socketDidDisconnect withError: %@", sock, err);
    }
    
    - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
    {
        NSString* newStr = [NSString stringWithUTF8String:[data bytes]];    
        NSLog(@"socket socketDidReadData:%@", newStr);
    }
    
    -(IBAction)disconnect:(id)sender { }
    
    #pragma mark View stuff
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }
    
    - (void)viewDidUnload {}
    
    - (void)dealloc {
        self.socket = nil;
        [super dealloc];
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.