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
과 : 코드는 매우 간단하고 제공된 샘플을 기반으로하는 대신, 불행하게도
- (IBAction)connect:(id)sender {
IBStore *client = [[IBStore alloc]init];
[client connect];
}
:이 IBStore 메인 뷰 컨트롤러에서 인스턴스화하는 방법입니다
#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);
}
}
didConnectToHost
을 실행하면 GCDAsyncSocket.m이 실행될 때 앱이 작동을 멈 춥니 다 (socket4FD = socket(AF_INET, SOCK_STREAM, 0);
왜 이런 일이 일어나는지에 대한 아이디어는 높이 평가 될 것입니다. 고맙습니다!
BSD 소켓에 대해 자세히 알아보고 매달려 ... 내가 아는 한 거기에 걸려서는 안되기 때문에 이상한 일이 일어나고 있습니다. 이 함수에 대한 정보는 다음과 같습니다. http://en.wikipedia.org/wiki/Berkeley_sockets#socket.28.29 – MechEthan