I'm working on print functionality using socket programming in iOS. I'm using the following code to open streams and perform read and write operations. While opening streams, the delegate gets called for open event for both input and output streams. I also get a NSStreamEventHasSpaceAvailable
event where I perform a write operation and NSStreamEventHasBytesAvailable
event where I perform a read operation. Write operation is happening successfully for the first 2 times and after that I get bytes available event where I'm trying to perform a read operation. Everytime I get -1 as bytes read and getting a NSStreamEventErrorOccurred
event with a message
"The operation couldn’t be completed. Connection reset by peer"
for NSInputStream
followed by the same event for NSOutputStream
as well while performing to write for the 3rd time. Sometimes I even get Broken Pipe error as well.
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"123.123.12.12",DEFAULT_LPR_PORT, &readStream, &writeStream);
self.inputStream = (__bridge NSInputStream *)readStream;
self.outputStream = (__bridge NSOutputStream *)writeStream;
[self.inputStream setDelegate:self];
[self.outputStream setDelegate:self];
[self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.inputStream open];
[self.outputStream open];
Stream event handler - Delagate method code Bytes read is always -1.
case NSStreamEventHasBytesAvailable:
{
NSLog(@"NSStreamEventHasBytesAvailable");
if (theStream == inputStream) {
uint8_t buffer[1024];
int len = 0;
while ([self.inputStream hasBytesAvailable]) {
len = [self.inputStream read:buffer maxLength:sizeof(buffer)];
NSLog(@"bytes read len --- :%d ",len);
if (len > 0) {
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSUTF8StringEncoding];
if (nil != output) {
NSLog(@"bytes value: %@", output);
}
}
}
}
}
break;
Errror:
2013-09-18 12:27:36.424 SPConnector[1936:907] stream:handleEvent: : <__NSCFInputStream: 0x1e07b0e0>
2013-09-18 12:27:36.428 SPConnector[1936:907] NSStreamEventErrorOccurred localizedDescription --- The operation couldn’t be completed. Connection reset by peer
2013-09-18 12:27:36.430 SPConnector[1936:907] NSStreamEventErrorOccurred domain --- NSPOSIXErrorDomain
2013-09-18 12:27:36.431 SPConnector[1936:907] NSStreamEventErrorOccurred Code --- :54
2013-09-18 12:27:36.432 SPConnector[1936:907] stream:handleEvent: : <__NSCFOutputStream: 0x1e07b170>
2013-09-18 12:27:36.433 SPConnector[1936:907] NSStreamEventErrorOccurred localizedDescription --- The operation couldn’t be completed. Connection reset by peer
2013-09-18 12:27:36.434 SPConnector[1936:907] NSStreamEventErrorOccurred domain --- NSPOSIXErrorDomain
2013-09-18 12:27:36.435 SPConnector[1936:907] NSStreamEventErrorOccurred Code --- :54
Can someone let me know what could be the reason for the issue?