简体   繁体   English

iOS:当我尝试发送消息时,无法使用NSOutputStream连接将消息发送到套接字服务器

[英]iOS:Unable to send message to socket server using NSOutputStream connection drops when i try to send message

I have used following code to connect to the server 我已经使用以下代码连接到服务器

- (void) initNetworkCommunication
{
  CFReadStreamRef readStream;
  CFWriteStreamRef writeStream;
  CFStreamCreatePairWithSocketToHost(NULL,(CFStringRef)URLBASE, 8080, &readStream, &writeStream);

  inputStream = (__bridge_transfer NSInputStream *)readStream;
  outputStream = (__bridge_transfer NSOutputStream *)writeStream;

  [inputStream setDelegate:self];
  [outputStream setDelegate:self];
  [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  //    open input and output stream of socket to send and receive data
  [inputStream open];
  [outputStream open];
}

and following code to send message using output stream 和以下代码以使用输出流发送消息

-(IBAction)sendMessage{
   if (outputStream.hasSpaceAvailable) {
      [outputStream write:[data bytes] maxLength:[data length]];
   }
}

but when I try to send message the connection drops and gives connection reset by remote peer. 但是当我尝试发送消息时,连接断开,并由远程对等方重置连接。

when I try to send message the connection drops and gives connection reset by remote peer. 当我尝试发送消息时,连接断开,并由远程对等方重置连接。

The error text unequivocally states that the peer dropped the connection, so you have to look there for the reason rather than in the code above. 错误文本明确指出对等方断开了连接,因此您必须在此处查找原因而不是上面的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM