2011-09-23 36 views
2

我成功地打开了一个套接字发送数据给它。当我试图发送图像它崩溃,当我发送字符串它它工作正常。我将图像转换为数据,然后将这些数据放入必须发送到服务器的字符串中。使用套接字流传输图像或视频到服务器

需要指导,请帮助

下面是代码,我使用与流

NSString *urlStr = @"http://182.71.22.107:1935/VideoCalling/5d14a9bc-b816-4c82-bbb7-623d18243a02.sdp/playlist.m3u8"; 

if (![urlStr isEqualToString:@""]) 
{ 


    NSURL *website = [NSURL URLWithString:urlStr]; 

    if (!website) 
    { 

     NSLog(@"%@ is not a valid URL"); 

     return; 

    } 

    NSHost *host = [NSHost hostWithName:[website host]]; 

    // iStream and oStream are instance variables 

    [NSStream getStreamsToHost:host port:8081 inputStream:&iStream outputStream:&oStream]; 

    [iStream retain]; 

    [oStream retain]; 

    [iStream setDelegate:self]; 

    [oStream setDelegate:self]; 
     NSData *data = UIImageJPEGRepresentation([UIImage imageNamed:@"abc.png"], 90); 
     // Convert from host to network endianness 
     uint32_t length = (uint32_t)htonl([data length]); 
    // Don't forget to check the return value of 'write' 
     [oStream write:(uint8_t *)&length maxLength:4]; 
     [oStream write:[data bytes] maxLength:length];//writes to stream 

    [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] 

         forMode:NSDefaultRunLoopMode]; 

    [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] 

         forMode:NSDefaultRunLoopMode]; 

    [iStream open]; 

    [oStream open]; 

} 

在这里,我写入流

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode { 
int byteIndex; 
switch(eventCode) { 
    case NSStreamEventHasSpaceAvailable: 

    { 
     if (stream == oStream) { 

      //NSString * str = [NSString stringWithFormat: 

//
连接// @ “sdsdfdfggghhfhfh”]; NSString * str = [[NSString alloc] initWithData:datap encoding:NSUTF16StringEncoding];

  NSLog(@"%@,lenght===%d",str,[str length]); 

      const uint8_t * rawstring = (const uint8_t *)[str UTF8String]; 

// [oStream write:datap maxLength:strlen(rawstring)]; [oStream write:rawstring maxLength:strlen(15)];

  [oStream close]; 

     } 

     UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"h" message:@"Available" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil]; 
     [a show]; 
     [a release]; 
    } 
     break; 
+1

您使用的编程语言?你的代码如何看? – harper

+0

使用目标c和开发iphone/ipad –

+0

编辑问题... –

回答

相关问题