试图让NSStream在NSlog中显示

时间:2014-02-19 09:31:31

标签: ios objective-c

我可以连接到流但我无法显示此信息。如果我使用终端连接,我可以看到xml格式的流,但是在这段代码中我想在NSLog中看到它

这是我的代码,任何帮助都会很棒

#import "_CViewController.h"


@interface _CViewController ()

@end

@implementation _CViewController
@synthesize inStreamDictionary=_inStreamDictionary;

-(NSMutableDictionary *)inStreamDictionary{
    if(!_inStreamDictionary){
        _inStreamDictionary=[[NSMutableDictionary alloc] init];
    }
    return _inStreamDictionary;
}

    - (void)viewDidLoad
{
    [super viewDidLoad];




    NSString *deviceAddress = @"192.168.0.5";
    int devicePort=80;


//open stream
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)deviceAddress, devicePort   , &readStream, &writeStream);

//CFReadStreamSetProperty(readStream,kCFStreamNetworkServiceTypeVoIP,kCFBooleanTrue);
//CFWriteStreamSetProperty(writeStream,kCFStreamNetworkServiceTypeVoIP,kCFBooleanTrue);

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

connectToServerUsingCFStream:portNo:



[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];


[inputStream open];
[outputStream open];

NSString *soapBody=
@"<s:Envelope><s:Body><u:Subscribe xmlns:u=\"urn:udi-com:service:X_Insteon_Lighting_Service:1\"><reportURL>REUSE_SOCKET</reportURL><duration>infinite</duration></u:Subscribe></s:Body></s:Envelope>\n";

NSString *msgLength = [NSString stringWithFormat:@"%d", [soapBody length]];
//NSLog(@"Length: %@", msgLength);
NSString *soapMessageComplete = [NSString stringWithFormat:

                                 @"Post /services HTTP/1.1\n"
                                 "Host: %@:%i\n"
                                 "Authorization: Basic YWRtaW46YWRtaW4=\n"
                                 //"TIMEOUT:Second-infinite\n"
                                 //"CALLBACK:<REUSE_SOCKET>\n"
                                 "Content-Length: %@\n"
                                 "Content-Type: text/xml; charset=\"utf-8\"\r\n\r\n%@"
                                 ,deviceAddress, devicePort, msgLength,soapBody
                                 ];


NSLog(@"Subscribe message: %@", soapMessageComplete);
// subscribe: send soap message
NSData *data = [[NSData alloc] initWithData:[soapMessageComplete dataUsingEncoding:NSUTF8StringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];

NSLog(@"%@", data);
// Do any additional setup after loading the view, typically from a nib.

}



-(void)endSubscription{
[inputStream close];
[outputStream close];
[inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream setDelegate:nil];
[outputStream setDelegate:nil];

inputStream = nil;
outputStream = nil;


}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

1 个答案:

答案 0 :(得分:0)

您似乎没有设置流的代理,因此我不清楚您将如何获取数据。

此外,您似乎可以使用NSURLRequest并以三行代码完成所有这些操作。