我正在使用套接字使用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: 12 2012-08-15 08:16:30.900 Sockets[34836:f803] Sending buffer of len: 0 2012-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周期来反复告诉您的代码,如果您没有任何要编写的内容,您可以写一些东西.