绑定套接字时出错:NSPOSIXErrorDomain代码= 1“不允许操作”

时间:2019-04-01 14:40:17

标签: objective-c gcdasyncsocket

我正在尝试对Mac OS X(Mojave 10.14.3)应用程序实施GCDAsyncSocket,以侦听来自localhost:port的数据。

问题是无论我选择哪个端口,我总是会收到此错误:

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

我已经尝试过this,但是没有用。

这是我在 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];
}

我不确定我的实现是否错误,还是应该获得任何类型的许可。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

您应启用其在应用沙箱中的网络功能

macOS Catalina版本10.15.3

111