[英]The operation couldn’t be completed. Connection reset by peer
I'm working on print functionality using socket programming in iOS.我正在使用 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.我还得到一个
NSStreamEventHasSpaceAvailable
事件,我在其中执行写操作,而NSStreamEventHasBytesAvailable
事件在我执行读操作时。 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.写操作在前 2 次成功发生,之后我在尝试执行读操作时收到可用字节事件。 Everytime I get -1 as bytes read and getting a
NSStreamEventErrorOccurred
event with a message每次我在读取字节时得到 -1 并收到带有消息的
NSStreamEventErrorOccurred
事件时
"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.为
NSInputStream
后跟相同的事件NSOutputStream
以及同时执行第三次写入。 Sometimes I even get Broken Pipe error as well.有时我什至也会遇到 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];
Stream event handler - Delagate method code Bytes read is always -1.流事件处理程序 - 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
Can someone let me know what could be the reason for the issue?有人可以让我知道问题的原因是什么吗?
I receive the same message.我收到同样的消息。 I call my service with "getHandShakeParameter" string, but his name was "GetHandShakeParameter", with "G" in uppercase...
我用“getHandShakeParameter”字符串调用我的服务,但他的名字是“GetHandShakeParameter”,大写的“G”...
Problem solved ;)问题解决了 ;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.