gpt4 book ai didi

ios - 如何使用 NSInputStream 和 NSOutputStream

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:47:51 24 4
gpt4 key购买 nike

我正在尝试在 iPhone 之间创建一个简单的多人回合制游戏。现在我想做的就是将一些字符串传递给我的方法,然后让方法通过 NSOutputStream 发送字符串。 .我想我已经正确连接了我的 NSNetServices使用 NSNetServiceBrowser .一旦他们连接,我的 NSNetServiceDelegatenetService:didAcceptConnectionWithInputStream:outputStream:调用,这应该给我我的 i/o NSStream 对。我的方法如下所示:

-(void)netService:(NSNetService *)sender didAcceptConnectionWithInputStream:(NSInputStream *)inputStream outputStream:(NSOutputStream *)outputStream{
[self.myNet getInputStream:&inputStream outputStream:&outputStream];
self.inStream = inputStream;
self.outStream = outputStream;
[self.inStream setDelegate:self];
[self.inStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.inStream open];
}

我想我已经正确设置了 NSInputStream。我还有一个 NSStream 的代表,它正在实现 stream:handleEvent:

看起来像这样:

-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode{
NSInputStream *inStream = (NSInputStream *)aStream;
BOOL shouldClose = NO;
switch(eventCode) {
case NSStreamEventEndEncountered:
shouldClose = YES;
// If all data hasn't been read, fall through to the "has bytes" event
if(![inStream hasBytesAvailable]) break;
case NSStreamEventHasBytesAvailable: ; // We need a semicolon here before we can declare local variables
uint8_t *buffer;
NSUInteger length;
BOOL freeBuffer = NO;
// The stream has data. Try to get its internal buffer instead of creating one
if(![inStream getBuffer:&buffer length:&length]) {
// The stream couldn't provide its internal buffer. We have to make one ourselves
buffer = malloc(BUFFER_LEN * sizeof(uint8_t));
freeBuffer = YES;
NSInteger result = [inStream read:buffer maxLength:BUFFER_LEN];
if(result < 0) {
// error copying to buffer
break;
}
length = result;
}
// length bytes of data in buffer
if(freeBuffer) free(buffer);
break;
case NSStreamEventErrorOccurred:
// some other error
shouldClose = YES;
break;
case NSStreamEventHasSpaceAvailable:
break;
case NSStreamEventNone:
break;
case NSStreamEventOpenCompleted:
break;
}
if(shouldClose){
[inStream close];
}
}

我从中获取了该代码:this page .在该代码中,aStream应该是 self.inStream .我查看了描述如何处理 NSOutputStream 的页面,但它们似乎都不适合像我这样的初学者。我有几个问题。首先,我如何设置一种方法,将数据传入( NSDataNSString )并通过 self.outStream 发送出去.我更喜欢一个解释代码的答案,而不是仅仅把它给我。其次,我是否应该在 netService:didAcceptConnectionWithInputStream:outputStream: 中打开我的两个流? ,我也应该scheduleInRunLoop NSOutputStream self.outStream`。最后,我做错了吗?

感谢您的回答。这也是我的第一个问题,所以欢迎任何建设性的批评。

最佳答案

要写入输出流,检查流是否有可用空间并使用写入数据

- (NSInteger)write:(const uint8_t *)buffer maxLength:(NSUInteger)length

因此,代码可能如下所示。

if ( self.outStream.hasSpaceAvailable ) [ self.outStream write:... maxLength: ];

如果输出流没有可用空间,稍后

-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode

输出流委托(delegate)的方法将使用 eventCode = NSStreamEventHasSpaceAvailable 调用。

您应该打开输出流并将其安排在 netService:didAcceptConnectionWithInputStream:outputStream 或当您收到输入流的 NSStreamEventOpenCompleted 时。两者都可以。

如果不想写block thread,需要准备queue。

所以代码如下

    NSMutableData*  uQueue;    NSInputStream*  uIStream;    NSOutputStream* uOStream;    :    :    :    uIStream.delegate = self;    uOStream.delegate = self;    [ uIStream scheduleInRunLoop:NSRunLoop.currentRunLoop forMode:NSDefaultRunLoopMode ];    [ uOStream scheduleInRunLoop:NSRunLoop.currentRunLoop forMode:NSDefaultRunLoopMode ];    [ uIStream open ];    [ uOStream open ];    uQueue = NSMutableData.data;    :    :    :-   (void)Send{   if ( uQueue.length )    {   NSInteger wLength = [ uOStream write:(const uint8_t*)uQueue.bytes maxLength:uQueue.length ];        if ( wLength > 0 ) [ uQueue replaceBytesInRange:NSMakeRange( 0, wLength ) withBytes:NULL length:0 ];    }}-   (void)Write:(NSData*)p{   [ uQueue appendData:p ];    if ( uOStream.hasSpaceAvailable ) [ self Send ];}-   (void)stream:(NSStream*)pShandleEvent:(NSStreamEvent)p{   switch( p )    {    :    :    :    case NSStreamEventHasSpaceAvailable:        [ self Send ];        break;    }}

关于ios - 如何使用 NSInputStream 和 NSOutputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23186152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com