我正在使用套接字使用CFStreamCreatePairWithSocketToHost()发送和接收数据,并试图在发送多组数据(即不仅仅是1个请求)时如何完成此 *** 作.
问题
目前我可以发送数据并接收响应(即1次往返).但是,在我发送outputStream中的所有数据后,流被关闭(即接收NsstreamEventEndEncountered).
题
所以问题是,当我想发送多个数据请求时会发生什么?
>每次要发送新数据对象时,是否设置了新套接字?
>我是否必须重置outputStream并发送更多数据.
码
大部分代码来自Cocoa Streams Documentation:
- (voID)vIEwDIDLoad { [super vIEwDIDLoad]; // Do any additional setup after loading the vIEw,typically from a nib. _data = [[NSMutableData alloc] init]; [self initNetworkCommunication]; [self sendString:@"Hello World!"];}- (voID)initNetworkCommunication { CFReadStreamRef readStream; CFWriteStreamRef writeStream; CFStreamCreatePairWithSocketToHost(NulL,(CFStringRef)@"123.456.0.0",1234,&readStream,&writeStream); inputStream = (NSinputStream *)readStream; // ivar [inputStream setDelegate:self]; [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [inputStream open]; outputStream = (NSOutputStream *)writeStream; // ivar [outputStream setDelegate:self]; [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [outputStream open];}- (voID)sendString:(Nsstring *)string { NSData *data = [[NSData alloc] initWithData:[string dataUsingEnCoding:NSASCIIStringEnCoding]]; [_data appendData:data]; [data release];}- (voID)stream:(Nsstream *)theStream handleEvent:(NsstreamEvent)streamEvent { NSLog(@"stream event %u",streamEvent); switch (streamEvent) { case NsstreamEventopenCompleted: NSLog(@"Stream opened"); break; case NsstreamEventHasspaceAvailable: { uint8_t *readBytes = (uint8_t *)[_data mutableBytes]; readBytes += byteIndex; // ivar int data_len = [_data length]; unsigned int len = ((data_len - byteIndex >= 1024) ? 1024 : (data_len - byteIndex)); uint8_t buf[len]; (voID)memcpy(buf,readBytes,len); len = [(NSOutputStream *)theStream write:(const uint8_t *)buf maxLength:len]; NSLog(@"Sending buffer of len: %d",len); byteIndex += len; break; } case NsstreamEventHasBytesAvailable: if (theStream == inputStream) { uint8_t buffer[1024]; int len; while ([inputStream hasBytesAvailable]) { len = [inputStream read:buffer maxLength:sizeof(buffer)]; if (len > 0) { Nsstring *output = [[Nsstring alloc] initWithBytes:buffer length:len enCoding:NSASCIIStringEnCoding]; if (nil != output) { NSLog(@"server saID: %@",output); } } } [self sendString:@"Another Test"]; } break; case NsstreamEventErrorOccurred: NSLog(@"Can not connect to the host!"); break; case NsstreamEventEndEncountered: NSLog(@"Closing stream..."); [theStream close]; [theStream removeFromrunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [theStream release]; theStream = nil; break; default: NSLog(@"UnkNown event"); }}
响应:
2012-08-15 08:16:30.896 Sockets[34836:f803] Opened input stream.2012-08-15 08:16:30.898 Sockets[34836:f803] Opened output stream.2012-08-15 08:16:30.899 Sockets[34836:f803] Sending buffer of len: 122012-08-15 08:16:30.900 Sockets[34836:f803] Sending buffer of len: 02012-08-15 08:16:30.901 Sockets[34836:f803] Closing output stream.2012-08-15 08:16:30.939 Sockets[34836:f803] server saID: Hello World!
请注意,在发送数据后,outputStream流将关闭.我尝试在[self sendString:@“Another Test”]之前重新启动outputStream.我也试过了IDz的回答.
根据文档,我相信len:0的发送缓冲区是我的问题.
If the delegate receives an NsstreamEventHasspaceAvailable event and
does not write anything to the stream,it does not receive further
space-available events from the run loop until the NSOutputStream
object receives more bytes. When this happens,the run loop is
restarted for space-available events.
但是,文档没有说明在到达流末尾时关闭流.所以我很困惑……
解决方法 套接字是连接两个程序的双向流,可能在两台不同的计算机上.它只是将您在一端写入的数据传输到另一端进行读取.它在数据中不强制执行任何结构,并且不了解有关请求或响应的任何信息.CFStreamCreatePairWithSocketToHost API将单个连接拆分为两个独立的流 – 一个可以读取,另一个可以写入.这是一个很好的接触,底层套接字API只使用一个文件描述符进行读取和写入,这可能会让人非常困惑.
连接保持打开,直到一侧关闭插座.还有可能仅在一个方向上关闭插座.也可以仅在一个方向上关闭连接.如果遥控器关闭它的读取流,您的写入流将被关闭,反之亦然.
Do I setup a new socket every time I have a new data object to send?
你应该避免这样做.建立新连接需要一些时间,并且在连接达到全速之前需要更多时间.因此,您应该尽可能多地重用相同的连接.
Do I have to reset outputStream and send more data.
不,这不是必需的,只需发送更多数据.
Per the documentation,I belIEve the Sending buffer of len: 0 is my
problem.
什么都不写(这是一个长度为0的缓冲区)应该不是问题.文档没有说明会发生什么.所以我今天写了一个测试程序,看看会发生什么,什么也不期待.事实证明,写入长度为0的缓冲区会关闭输出流.所以这真的是你的问题.我将在Apple Bug Reporter上提出有关该文档问题的错误,您也应如此.
您引用的文档部分是关于不同的东西.如果您在获得可用空间通知后没有写信,那么在您写完文章之前,您将无法获得另一个通知.这很有用,因为系统不会浪费cpu周期来反复告诉您的代码,如果您没有任何要编写的内容,您可以写一些东西.
总结以上是内存溢出为你收集整理的iOS:使用CFStreamCreatePairWithSocketToHost的套接字网络基础全部内容,希望文章能够帮你解决iOS:使用CFStreamCreatePairWithSocketToHost的套接字网络基础所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)