Note that there are some explanatory texts on larger screens.

plurals
  1. PONSStream closing too soon
    primarykey
    data
    text
    <p>I have been doing a client-server communication for a while in iOS but I am here faced to an issue I have some troubles to understand.</p> <p>I wrote two basic functions: one to send data to the server and the other to receive data from the server. Each one has a parameter called timeout which allows to make the current thread sleep and wake up every 0.25s until the timeout is reached:</p> <pre><code>-(ReturnCode) send : (NSData*)data :(int)timeOut { if(![self isConnectionOpened]) return KO; float timer = 0; while(![_outputStream hasSpaceAvailable]) { [NSThread sleepForTimeInterval:0.25]; timer+=0.25; if(timer == timeOut) break; } int ret = 0; if([_outputStream hasSpaceAvailable]){ int lg = [data length]; ret = [_outputStream write:[data bytes] maxLength:lg]; if(ret == -1) return KO; else return OK; } return TIMEOUT; } - (ReturnCode) receive : (NSData**)data : (int)timeOut : (int)maxLength { uint8_t buffer[maxLength]; int len; NSMutableData* dataReceived = [[NSMutableData alloc] init]; if(! [self isConnectionOpened]) return KO; float timer = 0; while(![_inputStream hasBytesAvailable]) { [NSThread sleepForTimeInterval:0.25]; timer+=0.25; if(timer == timeOut) break; } if(![_inputStream hasBytesAvailable]) return TIMEOUT; while ([_inputStream hasBytesAvailable]) { len = [_inputStream read:buffer maxLength:sizeof(buffer)]; if (len &gt; 0) { [dataReceived appendBytes:buffer length:len]; *data = dataReceived; return OK; } } return KO; } </code></pre> <p>With iPhone 4 + iOS6, everything is going fine. But under iOS7, for some fuzzy reasons, inputstream and outputstream are closing prematurely (NSStreamEventErrorOccurred raised). The fact is, if I put a breakpoint just before receiving data from server and make the code run, it works fine and reading/writing streams dont close wrongly.</p> <p>So I think there is a synchronisation problem but I dont understand why... If anyone has ideas, pls help... </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload