【问题标题】:iOS 7 sockets connection refused error 61iOS 7 套接字连接被拒绝错误 61
【发布时间】:2014-02-02 04:46:11
【问题描述】:

我是 iOS 编程新手,正在尝试制作一个简单的程序来演示如何建立套接字连接。我正在关注的教程在这里:http://www.devx.com/wireless/Article/43551

我正在尝试在localhost:8080 上建立套接字连接。该应用程序有一个文本视图,它应该显示我正在传递的数据,这只是我输入的一个随机常量字符串。我遇到的问题是 NSLog 中出现错误消息(两次),在尝试连接时显示 The operation couldn’t be completed. Connection refused (Code = 61),可能一次用于输入,一次用于输出。我在运行 Mavericks (OS X 10.9.1) 的 Mac 上关闭了防火墙。我不知道还能做些什么来尝试使连接不被拒绝。非常感谢任何帮助。

下面是我正在使用的代码(这是我第一次尝试 iOS 程序,所以它可能很丑):

#import "OXCTextAreaViewController.h"
#import <Foundation/Foundation.h>

@interface OXCTextAreaViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textArea;
@end

NSInputStream *inputStream;
NSOutputStream *outputStream;
NSMutableData *data;

@implementation OXCTextAreaViewController
@synthesize textArea;

- (void)connectSockets
{
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, 
                                      (CFStringRef) @"localhost", 
                                      8080, 
                                      &readStream, 
                                      &writeStream);
    inputStream = (__bridge NSInputStream *) readStream;
    outputStream = (__bridge NSOutputStream *) writeStream;
    [inputStream setDelegate:self];
    [outputStream setDelegate:self];

    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
        forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
        forMode:NSDefaultRunLoopMode];

    [outputStream open];
    [inputStream open];
}

-(void) writeToServer:(const uint8_t *) buf {
    [outputStream write:buf maxLength:strlen((char*)buf)];
}


- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {

    switch(eventCode) {
        case NSStreamEventOpenCompleted:
        {
            NSLog(@"NSStreamEventOpenCompleted");
            break;
        }

        case NSStreamEventEndEncountered:
        {
            NSLog(@"NSStreamEventEndEncountered");
            break;
        }

        case NSStreamEventHasSpaceAvailable:
        {
            NSLog(@"NSStreamEventHasSpaceAvailable");
            break;
        }

        case NSStreamEventNone:
        {
            NSLog(@"NSStreamEventNone");
            break;
        }

        case NSStreamEventErrorOccurred:
        {
            NSError* error = [stream streamError];
            NSString* errorMessage = 
                [NSString stringWithFormat:@"%@ (Code = %d)",
                [error localizedDescription],
                [error code]];            
            NSLog(errorMessage);
            break;
        }

        case NSStreamEventHasBytesAvailable:
        {
            NSLog(@"NSStreamEventHasBytesAvailable");
            if (data == nil) {
                data = [[NSMutableData alloc] init];
            }
            uint8_t buf[1024];
            unsigned int len = 0;
            len = [(NSInputStream *)stream read:buf maxLength:1024];
            if(len) {
                [data appendBytes:(const void *)buf length:len];
                int bytesRead = 0;
                bytesRead += len;
            } else {
                NSLog(@"No data.");
            }

            NSString *str = 
                [[NSString alloc] initWithData:data
                                      encoding:NSUTF8StringEncoding];
            NSLog(str);
            UIAlertView *alert = 
                [[UIAlertView alloc] initWithTitle:@"From server"
                                           message:str
                                          delegate:self
                                 cancelButtonTitle:@"OK"
                                 otherButtonTitles:nil];
            [alert show];

            data = nil;
        } break;
    }
}


-(void) disconnect {
    [inputStream close];
    [outputStream close];
}



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self connectSockets];
    const uint8_t *str =
    (uint8_t *) [@"alsdkjflsdfjlasdfjldskfsdlafdsak" UTF8String];
    [self writeToServer:str];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

【问题讨论】:

    标签: ios objective-c sockets


    【解决方案1】:

    我无法完成这项工作,但我找到了https://github.com/robbiehanson/CocoaAsyncSocket,它解决了我的问题。我现在可以在localhost:8080 上发送和接收数据了。

    【讨论】:

      猜你喜欢
      • 2020-10-18
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      • 2019-12-22
      • 2014-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多