该操作无法完成。连接由同行重置

时间:2013-09-18 07:13:31

标签: ios objective-c sockets

我正在使用iOS中的套接字编程来处理打印功能。我正在使用以下代码打开流并执行读写操作。在打开流时,委托会为输入和输出流调用open事件。我还得到一个NSStreamEventHasSpaceAvailable事件,我在其中执行写操作和NSStreamEventHasBytesAvailable事件,我执行读操作。写操作在前2次成功发生,之后我得到字节可用事件,我正在尝试执行读操作。每次我得到-1作为字节读取并获得一个NSStreamEventErrorOccurred事件,消息“操作无法完成。对象重置连接”对于NSInputStream,后面跟NSOutputStream的同一事件,同时执行第三次写入。有时我甚至也会收到Broken Pipe错误。

        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];

流事件处理程序 - Delagate方法代码 读取的字节总是-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

有人可以告诉我这个问题可能是什么原因吗?

1 个答案:

答案 0 :(得分:0)

我收到同样的消息。 我用“getHandShakeParameter”字符串调用我的服务, 但他的名字是“GetHandShakeParameter”,大写的“G”......

问题解决了;)