ios 无法完成操作,对等方重置了连接

y0u0uwnf  于 2022-11-26  发布在  iOS
关注(0)|答案(1)|浏览(203)

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

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方法代码Bytes read始终为-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;

错误:

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

有人能告诉我问题的原因吗?

nzrxty8p

nzrxty8p1#

我收到同样的消息。我用"getHandShakeParameter"字符串调用我的服务,但他的名字是"GetHandShakeParameter",大写的是"G"...
问题已解决;)

相关问题