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) {
NSString *msg = [[NSString alloc] initWithBytes: buffer length: len encoding: NSASCIIStringEncoding];
NSLog(@"\tcontained message: %@", msg);
[communicator received: msg];
[msg release];
}
if (len < 0) {
[communicator received: @"Error!"];
}
break;
}
`
It would be great if someone could help me!
m jae source
share