简体   繁体   English

如何正确使用流对象

[英]How to correctly use stream object

I'm trying to use the stream object (input/output) to communicate with a server. 我正在尝试使用流对象(输入/输出)与服务器进行通信。 But I'm not sure if I'm doing this well, or if I have to adapt what I've already done. 但是我不确定我是否做得很好,还是必须调整已经完成的工作。

At the moment in each view whom need to communicate with my server I'm openning a connection with this method : 目前在每个需要与我的服务器通信的视图中,我正在使用此方法打开连接:

- (void)initNetworkCommunication {
  CFReadStreamRef readStream;
  CFWriteStreamRef writeStream;
  CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)kServerIp, kServerPort, &readStream, &writeStream);
  inputStream = (__bridge NSInputStream *)readStream;
  outputStream = (__bridge NSOutputStream *)writeStream;

  [inputStream setDelegate:self];
  [outputStream setDelegate:self];

  [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

  [inputStream open];
  [outputStream open];
}

And I don't know if it's the right way to do it (I'm pretty sure it's not). 而且我不知道这是否是正确的方法(我很确定那不是)。 I'm also using JSON to communicate with my server (and again I don't know if the best way to do that, if it's not let me know) 我也在使用JSON与服务器通信(同样,我不知道这样做的最佳方法,如果不是让我知道的话)

So I'm wondering if you guys can teach me the right way to use stream object :) I've found this post Manage sockets in iOS with uitabbarcontroller but I don't really understand what I've to do. 因此,我想知道你们是否可以教给我使用流对象的正确方法:)我发现这篇文章使用uitabbarcontroller在iOS中管理套接字,但是我真的不明白我该怎么做。

ps I'm using stream to do queries on my database ie : app -> giveMeMyNews -> server -> yourNews -> app -> Display. ps我正在使用流对我的数据库进行查询,即:app-> GiveMeMyNews->服务器-> yourNews-> app->显示。 Pretty simple but again since I'm really new to iPhone developpement I don't know if I'm doing this right :( 很简单,但是又一次,因为我真的是iPhone开发的新手,所以我不知道我是否做对了:(

Streams are working such that you can either have an input stream or output stream. 流正在工作,因此您可以具有输入流或输出流。 The stream sends a delegate method when it is able to accept your bytes for sending or when there a new bytes available that where received. 当流能够接受要发送的字节或接收到的新字节可用时,该流将发送委托方法。

Have a look at my Bonjour writeup how I implemented the stream:handleEvent: method, that should clear things up: http://www.cocoanetics.com/2012/11/bonjour/ 看看我的Bonjour文章,我是如何实现stream:handleEvent:方法的,该方法应该可以解决问题: http : //www.cocoanetics.com/2012/11/bonjour/

Finally if you are looking to communicate via WiFi between iOS and/or Mac devices I wrote DTBonjour which greatly simplifies sending of objects. 最后,如果您希望在iOS和/或Mac设备之间通过WiFi进行通信,我写了DTBonjour,它大大简化了对象的发送。 You connect and send NSObjects, they automatically get encoded as plist or json and the recipient gets it as decoded object. 您连接并发送NSObject,它们会自动被编码为plist或json,而接收者会将其作为解码的对象。 http://www.cocoanetics.com/2012/11/and-bonjour-to-you-too/ http://www.cocoanetics.com/2012/11/and-bonjour-to-you-too/

In the least I believe that my code is sufficiently clear to help you understand the process. 至少我相信我的代码足够清楚,可以帮助您理解该过程。

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

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