2011-09-23 43 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

編輯問題... –

回答

相關問題