繁体   English   中英

尝试连接到iOS上的TCP服务器的SIGBART错误

[英]SIGBART error trying to connect to a TCP server on iOS

我正在使用CocoaAsyncSocket库尝试从我的iOS设备到某处的服务器建立简单的TCP连接,现在我正试图仅尝试检查设置并连接到远程服务器。 IP地址。

下载库之后,我打开了以下项目:

GCD-> Xcode-> ConnectTest->移动

我已经打开了这个示例,因为我认为这是最简单的示例,并展示了我需要它执行的操作。 我已经在编辑器中运行了程序而没有碰到代码,它可以按预期工作。 但是,当我将代码带到程序中时,出现以下日志错误:

     -[BTLECentralViewController window]: unrecognized selector sent to instance 0x17e2c460
     *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[BTLECentralViewController window]: unrecognized selector sent to instance 0x17e2c460'
    *** First throw call stack:
    (0x314a0e8b 0x3b79a6c7 0x314a47b7 0x314a2f55 0x313f1e98 0xc5e89 0x33c2ab3b 0x33c2a8f9 0x33db75a3 0x33cd51df 0x33cd4fe9 0x33cd4f7d 0x33c26533 0x338adf43 0x338a9767 0x338a95f9 0x338a900d 0x338a8e1f 0x338a2b4d 0x3146bf71 0x314698ff 0x31469c4b 0x313d4541 0x313d4323 0x3610b2eb 0x33c8b1e5 0xc4bb9 0x3bc93ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

与我从他们那里获取代码一样,在他们的AppDelegate类中建立了连接。 我已经把它放在要激活它的视图控制器中。

所以现在我的viewcontroller.h看起来像这样:

#import <UIKit/UIKit.h>
@class GCDAsyncSocket;
@class BTLECentralViewController;

@interface BTLECentralViewController : UIViewController <UIApplicationDelegate>
{
    GCDAsyncSocket *async_socket;
}

@property (nonatomic) IBOutlet BTLECentralViewController *view_controller;

@end

随附的.m文件如下。 我将只发布已为其添加新代码的代码方法。 我不会浪费时间参加整个课堂。

#import "GCDAsyncSocket.h"

#if USE_SECURE_CONNECTION
#define HOST @"www.paypal.com"
#define PORT 443
#else
#define HOST @"google.com";
#define PORT 80
#endif

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Start up the CBCentralManager
    _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];



    dispatch_queue_t mainQueue = dispatch_get_main_queue();

    async_socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];

#if USE_SECURE_CONNECTION
    {
        NSString *host = HOST;
        uint16_t port = PORT;

        DDLogInfo(@"Connecting to \"%@\" on port %hu...", host, port);
        self.viewController.label.text = @"Connecting...";

        NSError *error = nil;
        if (![asyncSocket connectToHost:@"www.paypal.com" onPort:port error:&error])
        {
            DDLogError(@"Error connecting: %@", error);
            self.viewController.label.text = @"Oops";
        }
    }
#else
    {
        NSString *host = HOST;
        uint16_t port = PORT;

        //  DDLogInfo(@"Connecting to \"%@\" on port %hu...", host, port);
        _estimote_3_label.text = @"Connecting...";

        NSError *error = nil;
        if (![async_socket connectToHost:host onPort:port error:&error])
        {
            //      DDLogError(@"Error connecting: %@", error);
            _estimote_3_label.text = @"Oops";
        }

        // You can also specify an optional connect timeout.

        //  NSError *error = nil;
        //  if (![asyncSocket connectToHost:host onPort:80 withTimeout:5.0 error:&error])
        //  {
        //      DDLogError(@"Error connecting: %@", error);
        //  }

    }
#endif


    // Normal iOS stuff...

    self.window.rootViewController = self.view_controller;
    [self.window makeKeyAndVisible];  
}

如我所说,我的希望是在调试日志中出现一个“已连接”字符串。 但是,当我运行它时,我得到了调试日志错误,并粘贴在顶部,而我的主类中出现了SIGBART错误,如下所示:

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

任何有使用该库的更多经验的人都可以帮助我,告诉我我做错了什么,为什么会出现这些错误?

请注意,在编辑器中,示例项目正常运行,并且在我的iPhone 5上运行正常。 只是在我将相关代码带到我的项目中时,我才遇到这些问题。

BTLECentralViewController和UIViewController没有您要使用的window属性:

self.window.rootViewController = self.view_controller;
[self.window makeKeyAndVisible];  

我认为那应该放在AppDelegate(UIResponder)类中。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM