I’m basically sending a post to my server. The string I’m writing is @”POST /api?f=### HTTP/1.1\r\nHost: http://server.loc.a.tion\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: ##\r\n\r\ndata=somedataandstuff”. After this the server is supposed to send me back a message. (not that I’ve got that far yet.)
I’m following the basic polling code as lined out by apple, but I’ve made some changes.
Note the following is after I’ve opened the streams), data_to_send.buffer is a (char *), and data_to_send.buffer_size is 1500.
//Preform Write operations
bool done = false;
int bufLen = data_to_send.buffer_size;
const uint8_t * rawstring =(const uint8_t *)data_to_send.buffer;
CFIndex bytesWritten;
while (!done) {
CFStreamStatus stat = CFWriteStreamGetStatus(writeStream); //for me while debugging
if (CFWriteStreamCanAcceptBytes(writeStream)) {
//ADDED:
if(bufLen > 0) bytesWritten = CFWriteStreamWrite(writeStream, rawstring, (CFIndex)bufLen);
//ADDED: to check if it ever writes
if( CFWriteStreamGetStatus(writeStream) == kCFStreamStatusWriting){
NSLog(@"lalallaa");
}
if (bytesWritten < 0) {
//error stuff
} else if (bytesWritten == 0) {
if (CFWriteStreamGetStatus(writeStream) == kCFStreamStatusAtEnd) {
done = TRUE;
rawstring = nil;
}
} else if (bytesWritten != bufLen) {
// Determine how much has been written and adjust the buffer
bufLen = bufLen - bytesWritten;
memmove(rawstring, rawstring + bytesWritten, bufLen);
//error stuff
} else { //ADDED
bufLen = 0;
bytesWritten = 0;
//loop and wait until finished
}
}
}
CFWriteStreamClose(writeStream);
CFRelease(writeStream);
writeStream = NULL;
I modified the code because CFWriteStreamWrite returns the full 1500 (this is a flag in my mind: if bufLen is 1500 and it writes it all shouldn’t it return 0?), which wasn’t accounted for in apples code (ie: bytesWritten == bufLen)(another flag as I assume apple wouldn’t miss anything unless this was the intended functionality). In turn the bufLen or the buffer (rawstring) don’t get modified and we just repeatedly return 1500, and commence a never ending cycle. So to account for this I added the else which sets bufLen to 0 and bytes written to 0 so that the code will loop to check when it reaches KCFStreamStatusAtEnd. Now when I step through this I noticed that once it does CFWriteStreamWrite the status is till 2 (CFStreamStatusOpen) and hence another never ending cycle. The check I added to see if it ever writes never gets hit so as far as I can tell it doesn’t.
But, now if we check the apache access log on the server we have:
… “-” 408 –
where a working call (made from working java code) would look like
… “POST /api?f=blahblah” 200 ##
so I feel like something is at least trying to be written. Although a 408 status is a Request times out. This could be because the stream sits there while I debug with the stream open? Or because when I debug and stop I never close the stream?
Any ideas or suggestions would be greatly appreciated. (I’m new to iOS/objective-c)
If anyone is interested in this I basically gave up and went with a NSMutableURLrequest.
Keep in mind if you want to do something similar that this is synchronous so it will delay your UI (so run in it’s own thread or do asynchronously). This basically does the same thing that I was trying to code.