Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSStream读取\在委托方法handleEvent外写入:eventCode_Iphone_Sockets_Nsstream - Fatal编程技术网

Iphone NSStream读取\在委托方法handleEvent外写入:eventCode

Iphone NSStream读取\在委托方法handleEvent外写入:eventCode,iphone,sockets,nsstream,Iphone,Sockets,Nsstream,在iPhone应用程序中,我有一个通过wifi的插座连接,我需要从inputStream读取数据并写入outputStream。问题是流管理是事件驱动的,在读取之前,我必须等待事件nsstreamventhasbytesavailable。所以我不知道什么时候在handleEvent:eventCode委托方法之外读/写 我尝试了一个while循环,但我意识到在while循环期间,应用程序不会接收代理消息,也不会停止: 伪代码: -(void) myFunction { canRead=N

在iPhone应用程序中,我有一个通过wifi的插座连接,我需要从inputStream读取数据并写入outputStream。问题是流管理是事件驱动的,在读取之前,我必须等待事件nsstreamventhasbytesavailable。所以我不知道什么时候在handleEvent:eventCode委托方法之外读/写

我尝试了一个while循环,但我意识到在while循环期间,应用程序不会接收代理消息,也不会停止:

伪代码:

-(void) myFunction {
   canRead=NO;
   [self writeToStream:someData];
   while(!canRead) { };
   readData=[self readFromStream];
}

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

    switch(eventCode) {
          case NSStreamEventHasBytesAvailable: {
        canRead=YES;
        break;
      }
       }
}
我想我可以在委托方法内读\写,但我需要在委托方法外读\写很多次

救命啊!
谢谢

stream类可能在EventQueue上放置了一个事件来调用“stream:handleEvent:”。如果代码无法将控制权返回给事件处理程序,则无法读取事件队列。您可能不想这样做的是:

见:

以及可可节目的一般概述:

-(void)myFunction1 {
  [self writeToStream:somedata];
}
-(void)myFunction2 {
  readData=[self readFromStream];
}
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
    switch(eventCode) {
          case NSStreamEventHasBytesAvailable: {
            [self myFunction2];
        break;
      }
       }
}