使用套接字流将图像或视频发送到服务器

时间:2011-09-23 07:16:19

标签: iphone objective-c sockets video streaming

我成功打开一个套接字来向它发送数据。当我尝试发送图像时它崩溃了,当我向它发送字符串时它工作正常。我正在将图像转换为数据,然后将这些数据放入字符串中必须发送到服务器。

需要指导,请帮助

下面是代码,我用来连接流

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 个答案:

答案 0 :(得分:0)