繁体   English   中英

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

[英]send image or video to a server using socket 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: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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM