简体   繁体   English

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

[英]Error while binding a socket : NSPOSIXErrorDomain Code=1 "Operation not permitted"

I'm trying to implement GCDAsyncSocket to mac os x (Mojave 10.14.3) application to listen data from localhost:port.我正在尝试将GCDAsyncSocket实现到 mac os x (Mojave 10.14.3) 应用程序以侦听来自 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 :这是我在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!任何帮助,将不胜感激!

You should enable its network capability of the App Sandbox您应该启用其应用沙盒的网络功能

macOS Catalina Version 10.15.3 : macOS Catalina 版本 10.15.3

111

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Voip应用程序IOS收到错误错误域= NSPOSIXErrorDomain代码= 61“该操作无法完成。连接被拒绝 - Voip app IOS getting error Error Domain=NSPOSIXErrorDomain Code=61 "The operation couldn’t be completed. Connection refused 域= NSPOSIXErrorDomain代码= 61 - Domain=NSPOSIXErrorDomain Code=61 无法在 swift 中使用 Process() 打开应用程序返回错误域 = NSPOSIXErrorDomain 代码 = 13“权限被拒绝” - Not able to open an app using Process() in swift returns Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied" Cocoa:iOS:fopen有时失败并出现错误:“操作不允许” - Cocoa: iOS: fopen fails sometimes with error: “Operation not permitted” “该操作无法完成。 仅将图像发送到icloud时,“不允许操作”错误 - “The operation couldn’t be completed. Operation not permitted” error when simply sending image to icloud NSPOSIXErrorDomain代码= 12 3G网络中的“无法分配内存” - NSPOSIXErrorDomain Code=12 “Cannot allocate memory” in 3G network 调用Web服务时出错,操作无法完成 - Error while calling web service, operation can not be completed 尝试订阅CBCharacteristic时,不允许写入错误 - Writing is not permitted error when attempting to subscribe to CBCharacteristic 错误域=NSURLErrorDomain 代码=-1017 “操作不能 - Error Domain=NSURLErrorDomain Code=-1017 "The operation couldn’t be NSInputStream:read:maxLength:在TCP套接字流上返回错误“操作无法完成。 错误的文件描述符” - NSInputStream:read:maxLength: on a TCP socket stream returns the error “The operation couldn’t be completed. Bad file descriptor”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM