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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:05:19+00:00 2026-05-15T12:05:19+00:00

I’ve been working with CFNetwork to create a client application for iPad, it is

  • 0

I’ve been working with CFNetwork to create a client application for iPad, it is a small application, nothing fancy.

This is how I create the connection to the server (running in windows).

- (void) startConnection
{
    char ip[] = "192.168.0.244";
    NSString *ipAddress = [[NSString alloc] initWithCString:ip];

    /* Build our socket context; this ties an instance of self to the socket */
    CFSocketContext CTX = { 0, self, NULL, NULL, NULL };

    /* Create the server socket as a TCP IPv4 socket and set a callback */
    /* for calls to the socket's lower-level connect() function */  
    TCPClient = CFSocketCreate(NULL, PF_INET, SOCK_STREAM, IPPROTO_TCP,
                               kCFSocketDataCallBack, (CFSocketCallBack)DataCallBack, &CTX);

    if (TCPClient == NULL)
        return;

    /* Set the port and address we want to listen on */
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_len = sizeof(addr);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);// PORT = 5001
    addr.sin_addr.s_addr = inet_addr([ipAddress UTF8String]);

    CFDataRef connectAddr = CFDataCreate(NULL, (unsigned char *)&addr, sizeof(addr));
    CFSocketConnectToAddress(TCPClient, connectAddr, -1);
    CFRunLoopSourceRef sourceRef =
    CFSocketCreateRunLoopSource(kCFAllocatorDefault, TCPClient, 0);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), sourceRef, kCFRunLoopCommonModes);
    CFRelease(sourceRef);
    CFRunLoopRun();
} 

As you can see, when I create the server socket I register a callback to listen to incoming data from the server.

Then I use the CFRunLoopAddSource(…) and CFRunLoopRun() methods to have this callback listen in the background.

Now, this is how it looks my send method

- (void) send
{
    if (TCPClient == NULL)
        return;

    Byte byteData[3];   
    for (int i = 1; i <= 25; i++) {
        receivedLastAnswer = NO;
        globalPos = i;
        byteData[0] = i;
        byteData[1] = 4;
        byteData[2] = 0;
        int len = 3;//strlen(byte)+1;               
        CFDataRef refData = CFDataCreate(kCFAllocatorDefault, byteData, len);
        CFSocketSendData(TCPClient, NULL, refData, 0);
        while (!receivedLastAnswer) {
            //this while is to wait until a response is send by the server
         }
    }
}

What I want to do is send a command to the server (the 3 bytes array you see is the command) and wait for the server to respond before sending the next command, to do this I use a while, the variable “receivedLastAnswer” is a flag that is set when my DataCallBack method is invoked, so if the server responds, the callback is invoked, it sets the flag to true and thus the while exits and the next command is send.

Of course this is not how I intend to leave the code, this is just for testing and to see if it works, cause later I will have to add another condition to the while, a timeout condition, in case the server doesn’t respond at all.

Now, my problem is that my application, the client, is not firing the callback method, and thus it just cycles infinitely; I think the reason for not firing the callback is because the application is busy in the while cycle, in fact if I remove the cycle, the callback is fired, but this doesn’t makes sense to me because isn’t the callback method supposed to be running on a different thread (isn’t that the purpose of CFRunLoopRun()?) and because it is running in a different thread it shouldn’t matter that the main thread be in a cycle or am I completely misunderstanding how this works?

Also, I tried not using a while cycle, but a Sleep() function, but it is the same, no callback is fired.

Do you have any idea why this may be happening or how can I accomplish this,

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-05-15T12:05:19+00:00Added an answer on May 15, 2026 at 12:05 pm

    Well I think that in deed my data Callback wont be called if the main thread is busy or blocked, so what I did was to leave the Callback method on the main thread and put my send method in another thread, it worked pretty well actually, however I’m still wandering if there would be another way, but for now it does what I need so I’m going to stick to it.

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

Sidebar

Ask A Question

Stats

  • Questions 428k
  • Answers 428k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Without trying it: I'd think that <xsl:when> cannot be a… May 15, 2026 at 1:24 pm
  • Editorial Team
    Editorial Team added an answer I'm afraid the answer is that you can't get what… May 15, 2026 at 1:24 pm
  • Editorial Team
    Editorial Team added an answer Sometimes a misbehaving client will get or send what it… May 15, 2026 at 1:24 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.