NSInputStream getBuffer: length: not working?

I really would like to use a method getBuffer:length:for NSInputStream. After many studies, I could not find the right example that uses this method, because most people really need it read: maxLength:.

So, now some facts about the environment: * I am developing an application for iPhone, iOS 3.1.3 * I have established network communication via sockets * This network connection really works; so I didn’t forget to add the stream to runloop or a valid delegate or such things - it already works * I only send and receive Strings over the network. * I installed a valid delegate that implements correctlystream: handleEvent:(distinguishes between received events and takes correct actions). I'm not sure if the receive code is 100% correct, as I sometimes get a message twice. It can also be caused by improper implementation of the device with which I communicate. To find out which of the latter points to this, I try to find out how many bytes really are in the receiving buffer when I receive the "NSStreamEventHasBytesAvailable" event. Since I do not know for sure that my implementation is correct, but I wanted to know the actual number of bytes received, I wanted to use it getBuffer: length:and look at the length later. The strange thing: the length is never printed on the console, as [((NSInputStream *) stream) getBuffer: &buf length: &numBytes]always evaluates to FALSE. In any case, part of the code subsequently works correctly, receives a message in the buffer and forwards it correctly - it works well. The question remains: Why doesn’t it work getBuffer: length:? Code of interest here:

`case NSStreamEventHasBytesAvailable: {

        uint8_t *buf;
        unsigned int numBytes;

        if ([((NSInputStream *) stream) getBuffer: &buf length: &numBytes]) {
            NSLog(@"\t\tBytes in the buffer: %i", &numBytes);
        }

        uint8_t buffer[BUFFER_SIZE];
        int len = [((NSInputStream *) stream) read: buffer 
                                         maxLength: BUFFER_SIZE];
        NSLog(@"\tread: %i bytes", len);

        /*
         if len > 0: len is equal to the filled byte elements
         if len == 0: end of buffer was reached while reading
         if len < 0: something terrible happened...
         */
        if (len > 0) {
            /* 1. create string from received byte buffer */
            NSString *msg = [[NSString alloc] initWithBytes: buffer length: len encoding: NSASCIIStringEncoding];
            NSLog(@"\tcontained message: %@", msg);
            /* 2. inform communicator about received message */
            [communicator received: msg];
            [msg release];
        }

        if (len < 0) {
            [communicator received: @"Error!"];
        }
        break;
    }

`

It would be great if someone could help me!

+3
source share
3 answers

Darwin is an open source, so "the truth is there." The source for NSStream shows that GSInetInputStream is a class that implements NSInputStream for a socket, and the getBuffer: length: implementation for this class will briefly answer the question:

- (BOOL) getBuffer: (uint8_t **)buffer length: (unsigned int *)len
{
  return NO;
}

Found here .

+10
source

, getBuffer , , , ...

0

ah, I think I understand what you mean. but I believe that the following code should then be evaluated to TRUE, at least after reading into the buffer? I just ask because it is not, getBuffer:length:still does not work. communication using accessible bytes works well, but the question remains ...

        uint8_t *buf1;
        unsigned int numBytes1;
        if ([((NSInputStream *) stream) getBuffer: &buf length: &numBytes]) {
            NSLog(@"\t\tBytes are in the buffer before Op.");
        }

        uint8_t buffer[BUFFER_SIZE];
        int len = [((NSInputStream *) stream) read: buffer 
                                         maxLength: BUFFER_SIZE];

        uint8_t *buf2;
        unsigned int numBytes2;
        if ([((NSInputStream *) stream) getBuffer: &buf2 length: &numBytes2]) {
            NSLog(@"\t\tBytes in the buffer after Op.");
        }

Sorry for creating an answer to my own question; but the comment was able to process so much code ...

0
source

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


All Articles