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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:17:55+00:00 2026-06-08T21:17:55+00:00

I am making an app with two components, an iPhone component and a Mac

  • 0

I am making an app with two components, an iPhone component and a Mac component. They are supposed to communicate with each other via bonjour. I use the following code on the Mac end to find a port for the service:

NSSocketPort *socket = [[NSSocketPort alloc] init];
struct sockaddr *addr = (struct sockaddr *)[[socket address] bytes];
int port = 9876;
if(addr->sa_family == AF_INET) {
    port = ntohs(((struct sockaddr_in *)addr)->sin_port);
} else if(addr->sa_family == AF_INET6) {
    port = ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
} else {
    [socket release];
    socket = nil;
    NSLog(@"The family is neither IPv4 nor IPv6. Can't handle!!!");
}

I can also use this code on the iPhone end and run the app in the simulator, and the connection works fine. However, when I tried to run this code on an actual iPhone, I discovered that NSSocketPort is not available on the iPhone. When I try to start the service with the port 9876, the Mac app displays a connection refused error when I try to connect with it. So how can I find a port to use without using NSSocketPort?

  • 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-08T21:17:56+00:00Added an answer on June 8, 2026 at 9:17 pm

    Ok I looked at Apple’s WiTap code and slightly modified it to write myself a method for getting a port:

    - (int) getPort {
    CFSocketContext socketCtxt = {0, self, NULL, NULL, NULL};   
    
    // Start by trying to do everything with IPv6.  This will work for both IPv4 and IPv6 clients 
    // via the miracle of mapped IPv4 addresses.    
    
    CFSocketRef witap_socket = CFSocketCreate(kCFAllocatorDefault, PF_INET6, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, nil, &socketCtxt);
    uint32_t protocolFamily;
    
    if (witap_socket != NULL)   // the socket was created successfully
    {
        protocolFamily = PF_INET6;
    } else // there was an error creating the IPv6 socket - could be running under iOS 3.x
    {
        witap_socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, nil, &socketCtxt);
        if (witap_socket != NULL)
        {
            protocolFamily = PF_INET;
        }
    }
    
    /*if (NULL == witap_socket) {
        if (error) *error = [[NSError alloc] initWithDomain:TCPServerErrorDomain code:kTCPServerNoSocketsAvailable userInfo:nil];
        if (witap_socket) CFRelease(witap_socket);
        witap_socket = NULL;
        return NO;
    }*/
    
    
    int yes = 1;
    setsockopt(CFSocketGetNative(witap_socket), SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes));
    
    // set up the IP endpoint; use port 0, so the kernel will choose an arbitrary port for us, which will be advertised using Bonjour
    if (protocolFamily == PF_INET6)
    {
        struct sockaddr_in6 addr6;
        memset(&addr6, 0, sizeof(addr6));
        addr6.sin6_len = sizeof(addr6);
        addr6.sin6_family = AF_INET6;
        addr6.sin6_port = 0;
        addr6.sin6_flowinfo = 0;
        addr6.sin6_addr = in6addr_any;
        NSData *address6 = [NSData dataWithBytes:&addr6 length:sizeof(addr6)];
    
        CFSocketSetAddress(witap_socket, (CFDataRef)address6);
        /*if (kCFSocketSuccess != CFSocketSetAddress(witap_socket, (CFDataRef)address6)) {
            if (error) *error = [[NSError alloc] initWithDomain:TCPServerErrorDomain code:kTCPServerCouldNotBindToIPv6Address userInfo:nil];
            if (witap_socket) CFRelease(witap_socket);
            witap_socket = NULL;
            return NO;
        }*/
    
        // now that the binding was successful, we get the port number 
        // -- we will need it for the NSNetService
        NSData *addr = [(NSData *)CFSocketCopyAddress(witap_socket) autorelease];
        memcpy(&addr6, [addr bytes], [addr length]);
        return ntohs(addr6.sin6_port);
    
    } else {
        struct sockaddr_in addr4;
        memset(&addr4, 0, sizeof(addr4));
        addr4.sin_len = sizeof(addr4);
        addr4.sin_family = AF_INET;
        addr4.sin_port = 0;
        addr4.sin_addr.s_addr = htonl(INADDR_ANY);
        NSData *address4 = [NSData dataWithBytes:&addr4 length:sizeof(addr4)];
    
        CFSocketSetAddress(witap_socket, (CFDataRef)address4);
        /*if (kCFSocketSuccess != CFSocketSetAddress(witap_socket, (CFDataRef)address4)) {
            if (error) *error = [[NSError alloc] initWithDomain:TCPServerErrorDomain code:kTCPServerCouldNotBindToIPv4Address userInfo:nil];
            if (witap_socket) CFRelease(witap_socket);
            witap_socket = NULL;
            return NO;
        }*/
    
        // now that the binding was successful, we get the port number 
        // -- we will need it for the NSNetService
        NSData *addr = [(NSData *)CFSocketCopyAddress(witap_socket) autorelease];
        memcpy(&addr4, [addr bytes], [addr length]);
        return ntohs(addr4.sin_port);
    }
    
    }
    

    I took out a lot of the error stuff so it’s probably not optimal, but it works.

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

Sidebar

Related Questions

I am making a iPhone app that has two different targets. They use the
I am making an iphone app that uses two fingers to place and scale
Iam making an app which has one index.php file. I also have these other
How to change sizes of ViewController? I'm making simple app which uses deliberately two
all. I'm making app TabBar with two views(ScrollView and TableView). When application launched, TabBar
I'm making an iPhone app in which I retrieve JSON data from http://maps.googleapis.com/maps/api/geocode/json?address=canada&sensor=true ,
I am making chat app with publisher and subscriber I got two class one
Here is the use case: I am making an app that will email HTML
I am making an iPhone app that will generate something based on quiz answers.
i'm making app based on tab bar with two tabs. My problem is that

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.