2009-10-24 2 views
0

SmallSockets 라이브러리 (http://smallsockets.sourceforge.net/)를 사용하여 앱을 만들고 있습니다. 22 :SmallSockets를 사용하여 소켓에서 읽으면 내 응용 프로그램이 손상됩니다.

2009-10-24 18 : 여기

#import "MAController.h" 
#import "Socket.h" 


@implementation MAController 

- (IBAction)doRequest:(id)sender 
{ 
    //Initialize 
    NSURL  *uUrl = [[NSURL alloc] initWithString:[tfHost stringValue]]; 
    int   iPort = [[uUrl port] intValue]; 
    NSString *sHost = [uUrl host]; 

    //Check 
    if(sHost == NULL) 
    { 
     //Invalid Host name 
     NSLog(@"Invalid Host name"); 
     NSAlert *aInvalidHost = [[[NSAlert alloc] init] autorelease]; 
     [aInvalidHost addButtonWithTitle:@"Ok"]; 
     [aInvalidHost setInformativeText:@"Invalid Host name"]; 
     [aInvalidHost setMessageText:@"You have entered an invalid host name. Please enter a valid host name."]; 
     [aInvalidHost setAlertStyle:NSInformationalAlertStyle]; 
     [aInvalidHost beginSheetModalForWindow:wMain modalDelegate:self didEndSelector:NULL contextInfo:nil]; 
     return; 
    } 
    if(iPort == 0) 
    { 
     iPort = 80; 
    } 

    //Create request 
    NSString *sRequest = [[NSString alloc] initWithString:[tvRequest string]]; 

    //Create socket 
    Socket *sSocket = [Socket socket]; 

    NSLog(@"Connecting..."); 
    [sSocket connectToHostName:sHost port:iPort]; 

    //Write request 
    NSLog(@"Writing..."); 
    [sSocket writeString:sRequest]; 

    //Read response 
    NSLog(@"Reading..."); 
    NSMutableData *mdResponse = [[[NSMutableData alloc] init] autorelease]; 

//IT CRASHES RIGHT HERE 
    while([sSocket readData:mdResponse]) 
    { 
     NSLog(@"Read."); 
    } 

    //Display response 
    NSLog(@"Displaying..."); 
    NSString *sResponse = [[[NSString alloc] initWithData:mdResponse 
              encoding:[NSString defaultCStringEncoding]] 
              autorelease]; 
    [tvResponse setString:sResponse]; 

    [mdResponse release]; 
    [sSocket release]; 
} 

@end 

내 디버거 내가 doRequest 동작을 발사 버튼을 클릭 한 후 말씀입니다 :

이 코드가 19.197 iDebug HTTP [4836 : a0f] 연결 중 ...
2009-10-24 18 : 22 : 19.399 쓰기 ...
2009-10-24 18 : 22 : 19.400 iDebug HTTP [4836 : a0f] 읽기 ...

호스트 이름 http://www.example.com
이며 포트는 80

입니다 내 응용 프로그램이 소켓 fromt 읽는 동안 중단 이유

사람이 나를 설명 할 수 있습니까? 미리 감사드립니다.

방화벽이 설치되어 있지 않습니다.

답변

0

이벤트 루프를 실행하지 않기 때문에; 방금 소켓에서 읽는 중입니다. tvRequest의 텍스트가 무엇인지 보여주지는 않지만, 출력을 기다리는 동안 서버가 추가 입력을 기다리는 것 같습니다.

NSURLConnection을 사용하면 더 쉽게 작업 할 수 있습니다. 비동기식이기 때문에 이벤트 루프를 사용하여 쉽게 재생할 수 있으며 요청 및 응답 클래스는 HTTP를 쉽게 만들 수 있도록 특별히 설계되었습니다.

관련 문제