Я пытаюсь реализовать приложение 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}
Я уже пробовал это , но это не сработало.
Вот моя реализация в 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];
}
Я не уверен, что моя реализация неверна или я должен получить какие-либо разрешения.
Любая помощь будет оценена!