0
我把我的網絡代碼分離到一個單獨的類IBStore中。該代碼是非常簡單的並且基於所提供的樣品:CocoaAsyncSocket在連接時崩潰
#import <UIKit/UIKit.h>
#import "GCDAsyncSocket.h"
@interface IBStore : UIViewController
{
GCDAsyncSocket *socket;
}
- (void)connect;
- (void)socket:(GCDAsyncSocket *)sender didConnectToHost:(NSString *)host port:(UInt16)port;
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag;
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag;
@end
和:
#import "IBStore.h"
@interface IBStore()
@end
@implementation IBStore
- (void)connect
{
socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *err = nil;
if (![socket connectToHost:@"127.0.0.1" onPort:80 error:&err]) // Asynchronous!
{
// If there was an error, it's likely something like "already connected" or "no delegate set"
NSLog(@"Connection error: %@", err);
}
}
這是如何IBStore從主視圖控制器實例:
- (IBAction)connect:(id)sender {
IBStore *client = [[IBStore alloc]init];
[client connect];
}
不幸的是,代替在執行didConnectToHost
時,該應用在執行時崩潰(掛起)在GCDAsyncSocket.m中socket4FD = socket(AF_INET, SOCK_STREAM, 0);
爲什麼會發生這種情況的任何想法將不勝感激。謝謝!
試着在BSD插座上查找更多信息並掛起...據我所知,它不應該掛在那裏,所以有些奇怪的事情正在發生。以下是關於該功能的一些信息:http://en.wikipedia.org/wiki/Berkeley_sockets#socket.28.29 – MechEthan