Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.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
ios套接字编程-NSStream不工作_Ios_Sockets_Nsstream - Fatal编程技术网

ios套接字编程-NSStream不工作

ios套接字编程-NSStream不工作,ios,sockets,nsstream,Ios,Sockets,Nsstream,我正在使用一个名为WiFly的设备,它的ip地址为169.254.1.1,端口为2000。我正在尝试通过iOS应用程序连接到此设备。我使用以下代码进行连接: CFReadStreamRef readStream; CFWriteStreamRef writeStream; UInt32 port = 2000; CFStringRef host = CFSTR("169.254.1.1"); CFStreamCreatePairWithSocketToHost(kCFAllocatorDef

我正在使用一个名为WiFly的设备,它的ip地址为169.254.1.1,端口为2000。我正在尝试通过iOS应用程序连接到此设备。我使用以下代码进行连接:

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;

UInt32 port = 2000;
CFStringRef host = CFSTR("169.254.1.1");

CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host, port, &readStream, &writeStream);

inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;

// set the delegates to this view controller
[inputStream setDelegate:self];
[outputStream setDelegate:self];

// Set run loops to continuous receive information
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

// Finally, open the connection
[inputStream open];
[outputStream open];
然后,我使用以下方法来处理流事件:

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
    NSLog(@"stream event %i", streamEvent);

    switch (streamEvent) {

        case NSStreamEventOpenCompleted:
            NSLog(@"Stream opened");
            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 messageReceived:output];
                        }
                    }
                }
            }
            break;

        case NSStreamEventErrorOccurred:
            NSLog(@"Can't connect to server");
            break;

        case NSStreamEventEndEncountered:
            [theStream close];
            [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            break;

        default:
            NSLog(@"Unknown event");
    }
因此,我可以看到前两个流是正确打开的。然后紧接着是流事件4,据我所知,这是意料之中的。但是,我尝试调用一个函数:

- (IBAction)moveForward
{
    NSLog(@"move forward called");
    NSString *response  = [NSString stringWithFormat:@"2"];
    NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
    [outputStream write:[data bytes] maxLength:[data length]];
}
从arduino uno通过wifly返回“前进”。但是,当我单击时,出于某种原因,我得到了另一个NS4。我还通过终端通过以下方式远程登录到设备:

telnet 169.254.1.1 2000

然后输入a“2”。。。这会立即返回所需的“转发”。从iPad的角度来看,我做错了什么

此外,这段代码在几周前就可以使用了。但一旦我更新了模拟器,它就停止工作了。。。连接正常打开,但arduino设备似乎没有从iOS获得输出

非常感谢您的帮助

尝试替换 NSString*响应=[NSString stringWithFormat:@“2”]; 用这个 NSString*响应=[NSString stringWithFormat:@“%d”,2];
在向前移动方法中。

nsstreamvent 4可用。 在发送数据之前,必须等待输出流具有可用空间