建立与流媒体服务器和 iPhone 的套接字连接

发布于 2024-11-06 18:59:43 字数 94 浏览 0 评论 0原文

我想与流媒体服务器(使用 iPhone)建立套接字连接,并希望将其内容(如图像、.css 等)下载到 iPhone。任何想法或示例代码都可以帮助我。我只需要为客户端编写代码。

I want to establish socket connection to streaming server (with iphone ) and want to download its content like image,.css,etc to iphone. Any Idea or sample code is can help me. I need to write code for client only.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尽揽少女心 2024-11-13 18:59:43

按如下方式建立连接并将 urlStr 更改为您的服务器 URL

    NSString *urlStr = @"http://192.168.0.108";
    NSURL *website = [NSURL URLWithString:urlStr];
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[website host], 1234, &readStream, &writeStream);
    NSInputStream *inputStream = (NSInputStream *)readStream;
    NSOutputStream *outputStream = (NSOutputStream *)writeStream;
    [inputStream setDelegate:self];
    [outputStream setDelegate:self];
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [inputStream open];
    [outputStream open];

使用 NSStream Delegate 如下读取数据

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {

    switch(eventCode) {
        case NSStreamEventHasBytesAvailable:

        {

            NSLog(@"Bytes Available");


            uint8_t b[1024];
            unsigned int len = 0;
            NSMutableData *data = [[NSMutableData alloc] init];
            len = [(NSInputStream *)stream read:b maxLength:1024];


            if(!len) {
                if ([stream streamStatus] != NSStreamStatusAtEnd)
                {

                }
            } else {

                [data appendBytes:(const void *)b length:len];
                int bytesRead;
                bytesRead += len;
               //make use of data here

        }
        }

            break;

    }

}

Establish Connection as follows and change the urlStr to your server URL

    NSString *urlStr = @"http://192.168.0.108";
    NSURL *website = [NSURL URLWithString:urlStr];
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[website host], 1234, &readStream, &writeStream);
    NSInputStream *inputStream = (NSInputStream *)readStream;
    NSOutputStream *outputStream = (NSOutputStream *)writeStream;
    [inputStream setDelegate:self];
    [outputStream setDelegate:self];
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [inputStream open];
    [outputStream open];

Make Use of NSStream Delegate as follows to read data

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {

    switch(eventCode) {
        case NSStreamEventHasBytesAvailable:

        {

            NSLog(@"Bytes Available");


            uint8_t b[1024];
            unsigned int len = 0;
            NSMutableData *data = [[NSMutableData alloc] init];
            len = [(NSInputStream *)stream read:b maxLength:1024];


            if(!len) {
                if ([stream streamStatus] != NSStreamStatusAtEnd)
                {

                }
            } else {

                [data appendBytes:(const void *)b length:len];
                int bytesRead;
                bytesRead += len;
               //make use of data here

        }
        }

            break;

    }

}
我不会写诗 2024-11-13 18:59:43

我使用的代码略有变化:

NSHost *host = [NSHost hostWithName:[website host]];
        [NSStream getStreamsToHost:host 
                      port:8766 
                       inputStream:iStream
                      outputStream:oStream];

而不是

CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[website host], 1234, &readStream, &writeStream);
    NSInputStream *inputStream = (NSInputStream *)readStream;
    NSOutputStream *outputStream = (NSOutputStream *)writeStream;

Slightly changes in the code I used:

NSHost *host = [NSHost hostWithName:[website host]];
        [NSStream getStreamsToHost:host 
                      port:8766 
                       inputStream:iStream
                      outputStream:oStream];

Instead of

CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[website host], 1234, &readStream, &writeStream);
    NSInputStream *inputStream = (NSInputStream *)readStream;
    NSOutputStream *outputStream = (NSOutputStream *)writeStream;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文