2

I'm trying to implement GCDAsyncSocket to mac os x (Mojave 10.14.3) application to listen data from localhost:port.

The problem is no matter what port I choose I always get this error:

Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"
UserInfo={NSLocalizedDescription=Operation not permitted, 
NSLocalizedFailureReason=Error in bind() function}

I've already tried this, but it didn't work.

Here is my implementation in AppDelegate.mm:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    listenSocket = [[GCDAsyncSocket alloc] initWithDelegate:(id<GCDAsyncSocketDelegate>) self delegateQueue:dispatch_get_main_queue()];
    connectedSockets = [[NSMutableArray alloc] initWithCapacity:1];
    isRunning = NO;
    NSError *err = nil;
    if (![listenSocket connectToHost:@"localhost" onPort:12345 error:&err]){
        NSLog(@"I goofed: %@", err);
    }
}

-(void)getData{
    if(!isRunning)
    {
        int port = 12345;
        if(port < 0 || port > 65535)
        {
            port = 0;
        }
        NSError *error = nil;
        if(![listenSocket acceptOnPort:port error:&error])
        {
            NSLog(@"error: %@", FORMAT(@"Error starting server: %@", error));
            return;
        }
        NSLog(@"error: %@", FORMAT(@"Echo server started on port %hu", [listenSocket localPort]));
        isRunning = YES;
    }
    else
    {
        // Stop accepting connections
        [listenSocket disconnect];

        // Stop any client connections
        NSUInteger i;
        for(i = 0; i < [connectedSockets count]; i++)
        {
            [[connectedSockets objectAtIndex:i] disconnect];
        }
        NSLog(@"Stopped Echo server");
        isRunning = false;
    }
}

- (void)onSocket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
    [connectedSockets addObject:newSocket];
}

- (void)onSocket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
    NSLog(FORMAT(@"Accepted client %@:%hu", host, port));
    NSString *welcomeMsg = @"Welcome to the AsyncSocket Echo Server\r\n";
    NSData *welcomeData = [welcomeMsg dataUsingEncoding:NSUTF8StringEncoding];

    [sock writeData:welcomeData withTimeout:-1 tag:WELCOME_MSG];

    [sock readDataToData:[GCDAsyncSocket CRLFData] withTimeout:READ_TIMEOUT tag:0];
}

- (void)onSocket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
    if(tag == ECHO_MSG)
    {
        [sock readDataToData:[GCDAsyncSocket CRLFData] withTimeout:READ_TIMEOUT tag:0];
    }
}

- (void)onSocket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length] - 2)];
    NSString *msg = [[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding];
    if(msg)
    {
        NSLog(@"msg %@", msg);
    }
    else
    {
        NSLog(@"Error converting received data into UTF-8 String");
    }

    // Even if we were unable to write the incoming data to the log,
    // we're still going to echo it back to the client.
    [sock writeData:data withTimeout:-1 tag:ECHO_MSG];
}

- (NSTimeInterval)onSocket:(GCDAsyncSocket *)sock
  shouldTimeoutReadWithTag:(long)tag
                   elapsed:(NSTimeInterval)elapsed
                 bytesDone:(NSUInteger)length
{
    if(elapsed <= READ_TIMEOUT)
    {
        NSString *warningMsg = @"Are you still there?\r\n";
        NSData *warningData = [warningMsg dataUsingEncoding:NSUTF8StringEncoding];

        [sock writeData:warningData withTimeout:-1 tag:WARNING_MSG];

        return READ_TIMEOUT_EXTENSION;
    }

    return 0.0;
}

- (void)onSocket:(GCDAsyncSocket *)sock willDisconnectWithError:(NSError *)err
{
    NSLog(FORMAT(@"Client Disconnected: %@:%hu", [sock connectedHost], [sock connectedPort]));
}

- (void)onSocketDidDisconnect:(GCDAsyncSocket *)sock
{
    [connectedSockets removeObject:sock];
}

I'm not sure about if my implementation is wrong or I should get any kind of permissions granted.

Any help would be appreciated!

1 Answer 1

3

You should enable its network capability of the App Sandbox

macOS Catalina Version 10.15.3:

111

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.