write() causes fatal crash when filedescriptor becomes invalid
Posted
by ckrames1234
on Stack Overflow
See other posts from Stack Overflow
or by ckrames1234
Published on 2010-04-01T00:36:54Z
Indexed on
2010/04/01
0:43 UTC
Read the original article
Hit count: 406
I'm writing an iPhone App with a webserver in it. To handle a web request, I take the web request and write() to it the data that I want to send back.
When I try to download a moderately sized file (3-6MB) it works fine, but if I cancel the download halfway through, the app crashes and leaves no trace of an error. I'm thinking that the file descriptor becomes invalid halfway through the write, and causes the crash. I really don't know if this is what causes the crash, i'm just assuming.
I'm basing my webserver off of this example.
NSString *header = @"";
NSData *data = [NSData dataWithContentsOfFile:fullPath];
write (fd, [header UTF8String], [header length]);
write(fd, [data bytes], [data length]);
close(fd);
Does anyone know how to fix this? I was thinking about chunking the data and then writing each part, but I don't think it would help.
© Stack Overflow or respective owner