IPhone SDK - NSStreamEventHasBytesAvailable / appendBytes: failed

Disclaimer: I am the Nocode Xcode / iPhone SDK.

I am trying to establish a client TCP / IP connection with an existing server. When connecting, I expect to get some data about the server (version, etc.).

When my connection is completed, an event occurs NSStreamEventOpenCompleted, so I know that the connection is completed. Then the event fires NSStreamEventHasBytesAvailableand I execute the following code. The value 71 (int) is stored in len, which in my opinion is correct. However line

[data appendBytes:&buffer length:len];

crumbling (I think). There is no actual error, but I see , although I explicitly added the error: __TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__

case NSStreamEventHasBytesAvailable:
    {
        NSMutableData *data=[[NSMutableData alloc] init];

        uint8_t *buffer[1024];
        unsigned int len=0;

        len=[(NSInputStream *)stream  read:buffer maxLength:1024];
        if(len>0){  
            @try{
                [data appendBytes:&buffer length:len];
            }
            @catch(NSException *ex){
                NSLog(@"Fail: %@", ex); 
            }
            [statusLabel setText:[data stringValue]];
            //[bytesRead setIntValue:[bytesRead intValue]+len];
        }else{
            NSLog(@"No Buffer");
        }
        break ;
    }
+3
source share
3 answers

:

uint8_t buffer[1024];

:

[data appendBytes:buffer length:len];
+7

C: .

:

uint8_t *buffer[1024];

1024 uint8_t s, , . :

uint8_t buffer[1024];

- [NSMutableData appendBytes: length:], , : , :

[data appendBytes:buffer length:len];

, , .

__TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__, -, Objective-C @try/@catch; . , , .

+10

For completeness, read:maxLength:returns an NSInteger and will be negative if an error occurs. Assigning it to an unsigned int discards this and may cause a crash.

+2
source

Source: https://habr.com/ru/post/1706239/


All Articles