views:

15

answers:

0

Hi, i'm have problem with the writing TCP socket in objective c. i'm having the following error when i then to write a data to my server. my file size is 2727936 but while sending the data, the code cashed after sending 1000000+ byte. is something wrong with my code? or do i need to need something else? Thanks in advance. error=> malloc: * mmap(size=2727936) failed (error code=12) * error: can't allocate region

NSData *data = [NSData dataWithContentsOfFile:filepath];
int byteIndex = 0;
uint8_t *readBytes = (uint8_t *)[data bytes];
readBytes += byteIndex; 
int data_len = [data length];
NSLog(@"data_len = %d", data_len);
unsigned int len = ((data_len - byteIndex >= 1024) ?
                    1024 : (data_len-byteIndex));
uint8_t buf[len];
(void)memcpy(buf, readBytes, len);
len = [outputStream write:(const uint8_t *)buf maxLength:len];

if (-1 == len) {
    NSLog(@"Error writing to stream %@: %@", outputStream, [outputStream streamError]);
}

else{
    NSLog(@"Wrote %ld bytes to stream %@.", (long )len, outputStream);
}

byteIndex += len;