gpt4 book ai didi

objective-c - 绑定(bind)套接字时出错 : NSPOSIXErrorDomain Code=1 "Operation not permitted"

转载 作者:行者123 更新时间:2023-12-03 08:55:55 27 4
gpt4 key购买 nike

我正在尝试实现 GCDAsyncSocket到 mac os x (Mojave 10.14.3) 应用程序以监听来自 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];
}

我不确定我的实现是否错误或者我应该获得任何类型的权限授予。

如有任何帮助,我们将不胜感激!

最佳答案

您应该启用App Sandbox的网络功能

ma​​cOS Catalina 版本 10.15.3:

111

关于objective-c - 绑定(bind)套接字时出错 : NSPOSIXErrorDomain Code=1 "Operation not permitted",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55457744/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com