0

指定された IP アドレスとポート番号を持つマシンへのソケット接続を確立する必要がある IOS のストリーム プログラムで作業しています。最終的な目標は、応答ストリームを取得してクライアント マシンに表示することです。以下のコードを使用していますが、データ (バイト) を正しく読み取れません。NSStreamDelegate のストリーム ハンドラは「NSStreamEventHasBytesAvailable」イベントコードに到達しますが、データの読み取りに問題があります。誰かが問題を理解できれば、それは大きな助けになります

 -(IBAction)connectToServer:(id)sender

{
    NSString *portNo_=@"80";
    NSString *serviceName_=@"web";
    [self establishConnection:serviceName_ forIpAddress:@"`192.168.4.152/test.html`" forPort:portNo_];

}

- (void)establishConnection:(NSString*)serviceName forIpAddress:(NSString *)ipAddress forPort:(NSString *)portNo
{
     NSLog(@"connection method called");

    if(inputStream && outputStream)
    {    
        [self close];    
    }
    urlString = [NSString stringWithFormat:@"http://%@", ipAddress];   
    NSLog(@"Ditected URL is %@ ",urlString);
    NSURL *website = [NSURL URLWithString:urlString];

    if (!website)
    { 
        NSLog(@"%@ is not a valid URL", website);    
    }

    CFReadStreamRef readStream; 
    CFWriteStreamRef writeStream; 

    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[website host], [portNo intValue], &readStream, &writeStream);

    CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
    CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);

    inputStream = (NSInputStream *)readStream;
    outputStream = (NSOutputStream *)writeStream;    

    [inputStream setDelegate:self];
    [outputStream setDelegate:self];    

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

    [inputStream open];
    [outputStream open];    
}

- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode  
{
    NSLog(@"stream:handleEvent: is invoked..."); 
    NSError *theError;   
    NSMutableData *data=[[NSMutableData alloc]init];   
    NSString *event;  
    NSNumber *bytesRead=[[NSNumber alloc] init];

    switch (eventCode)
    {
        case NSStreamEventNone:                 
            event = @"NSStreamEventNone";   
            NSLog(@"event------%@",event);    
            break;   

        case NSStreamEventOpenCompleted:     
            event = @"NSStreamEventOpenCompleted";    
            NSLog(@"event------%@",event);   
            break;

        case NSStreamEventHasSpaceAvailable:    
            event = @"NSStreamEventHasSpaceAvailable";    
            NSLog(@"event------%@",event);    

            if (aStream == outputStream)   
            {
                NSString * str = [NSString stringWithFormat:@"GET / HTTP/1.0\r\n\r\n"];    
                const uint8_t *rawstring =(const uint8_t *)[str UTF8String];   
                [outputStream write:rawstring maxLength:strlen(rawstring)];   
                [outputStream close];  
            }    
            break;

        case NSStreamEventHasBytesAvailable:    
            event = @"NSStreamEventHasBytesAvailable";     
            NSLog(@"event------%@",event);

            if (aStream == inputStream)    
            {   
                uint8_t buf[1024];   
                unsigned int len = 0;

                len = [(NSInputStream *)aStream read:buf maxLength:1024];                    

                if(!data)
                {
                    data = [[NSMutableData data] retain];   
                }                

                @try    
                {   
                    if(len)   
                    {   
                        NSLog(@"length is --------------------%d", len);                        
                        [data appendBytes:(const void *)buf length:len];   
                        [bytesRead setIntValue:[bytesRead intValue]+len];   

                        NSLog(@"Bytes Read : %@ ",bytesRead);                        
                    }

                    else
                    {
                        NSLog(@"no buffer!");   
                    }

                }

           @catch (NSException *exception)
                    {
                    NSString *ex=[[NSString alloc]initWithFormat:@"Exception Occured:%@ . Reason:%@",[exception name],[exception reason]];

                    NSLog(@"%@",ex);
                }

                break; 
            }            

            case NSStreamEventErrorOccurred:
                theError = [aStream streamError];

                event = [[NSString alloc] initWithFormat:@"NSStreamEventErrorOccurred %@ ",theError];

                NSLog(@"event------%@",event);    
                [self close];   
                break;

            case NSStreamEventEndEncountered:
                event = @"NSStreamEventEndEncountered"; 
                NSLog(@"event------%@",event);

                [aStream close];
                [aStream removeFromRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];            
                [aStream release]; 
                aStream=nil;
                break;

            default:
               event=@"Unknown";
        }    
}

私の出力は次のようになります。

3-07-09 16:49:47.602 SocketProgram[4055:c07] 接続メソッドが呼び出されました 2013-07-09 16:49:47.604 SocketProgram[4055:c07] 検出された URL はhttp://192.168.4.152/test.html 618 SocketProgram[4055:c07] ストリーム:handleEvent: が呼び出されます... 2013-07-09 16:49:47.618 SocketProgram[4055:c07] イベント ------NSStreamEventHasBytesAvailable 2013-07-09 16:49:47.618 SocketProgram[4055:c07] バッファがありません! 2013-07-09 16:49:47.619 SocketProgram[4055:c07] ストリーム:handleEvent: が呼び出されます... 2013-07-09 16:49:47.619 SocketProgram[4055:c07] イベント------NSStreamEventEndEncountered

4

0 に答える 0