【问题标题】:Determine successful establishment of connection and broken connection via NSStream通过 NSStream 判断成功建立连接和断开连接
【发布时间】:2012-10-24 08:17:49
【问题描述】:

我正在尝试为NSStream 编写类似包装器的东西,以使我的生活更轻松。我只想知道什么时候建立连接,什么时候关闭,写一些数据来获取接收到的数据。所以我在想这样的事情:

标题:

@interface StreamWrapper : NSObject

- (id)initWithDelegate:(id <StreamWrapperDelegate>)delegate;
- (void)writeData:(NSData *)data;

@end

@protocol StreamWrapperDelegate <NSObject>

@required
- (void)streamWrapper:(StreamWrapper *)streamWrapper didReceiveData:(NSData *)data;
- (void)streamWrapperDidConnect:(StreamWrapper *)streamWrapper;
- (void)streamWrapperDidCloseConnection:(StreamWrapper *)streamWrapper;

@end

类:

@interface StreamWrapper () <NSStreamDelegate>

...
@property (nonatomic, weak) id <StreamWrapperDelegate> delegate;
@property (nonatomic, strong) NSInputStream *inputStream;
@property (nonatomic, strong) NSOutputStream *outputStream;

- (void)closeStreams;
- (void)setAndOpenStreams;

@end

@implementation StreamWrapper

#pragma mark - NSStreamDelegate

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

        case NSStreamEventEndEncountered:
            // Should I here close both streams (even if this is called only for one (in or out)) and delegate closed connection?
            break;

        case NSStreamEventErrorOccurred:
            // Should I here close both streams (even if this is called only for one (in or out)) and delegate closed connection?
            break;

        case NSStreamEventHasBytesAvailable:
            // My reading algorithm... 
            [self.delegate streamWrapper:self didReceiveData:data];
            break;

        case NSStreamEventHasSpaceAvailable:
            // Is this useful for me?
            break;

        case NSStreamEventNone:
            // Is this useful for me?
            break;

        case NSStreamEventOpenCompleted:
            // Should I here delegate successful connection? Should I wait to receive this for both streams? How?
            break;

        default:
            break;
    }
}

...

@end

所以...如何实现我将始终知道何时建立连接并且我能够发送和接收数据以及何时断开连接(即使只有一种方式)我应该关闭它并尝试全新的成立?或者这样做的正确方法是什么?

【问题讨论】:

    标签: ios macos sockets foundation cfnetwork


    【解决方案1】:

    根据Apple's Developer Website

     NSStreamEventNone
     No event has occurred.
    
     Available in OS X v10.3 and later.
    
     Declared in NSStream.h.
    
     NSStreamEventOpenCompleted
     The open has completed successfully.
    
     Available in OS X v10.3 and later.
    
     Declared in NSStream.h.
    
     NSStreamEventHasBytesAvailable
     The stream has bytes to be read.
    
      Available in OS X v10.3 and later.
    
     Declared in NSStream.h.
    
     NSStreamEventHasSpaceAvailable
     The stream can accept bytes for writing.
    
     Available in OS X v10.3 and later.
    
     Declared in NSStream.h.
    
     NSStreamEventErrorOccurred
     An error has occurred on the stream.
    
     Available in OS X v10.3 and later.
    
     Declared in NSStream.h.
    
     NSStreamEventEndEncountered
     The end of the stream has been reached.
    
     Available in OS X v10.3 and later.
    
     Declared in NSStream.h.
    
    • 也请参考这篇文章了解更多信息:

    Article : Byte-available event

    Article : space-available event

    【讨论】:

    • 我讨厌这种答案,但在这种情况下我应得的。
    • 如果它不能解决您的问题,我可以为您提供示例源代码。我正在研究的那个
    • 好吧,我想我现在可以做到,但如果可能的话,我也想看看你的解决方案。
    【解决方案2】:

    我之前使用 NSStream 的经验是捕获第一个 NSStreamEventHasSpaceAvailable 事件作为建立连接的信号。

    当出现 Error 或 Completed 消息时,我也将输入和输出流设为 nil - 所以我只需要在 writeData: 方法中检查这些流是否为 nil。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 2011-02-03
      • 2016-12-02
      • 2022-10-16
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      相关资源
      最近更新 更多