【问题标题】:Slow file transfer using Multipeer Connectivity framework使用 Multipeer Connectivity 框架的文件传输速度慢
【发布时间】:2013-10-18 19:58:08
【问题描述】:

我正在使用 iOS 7 的 Multipeer Connectivity Framework 在两台设备之间发送文件。我正在使用 NSStreams 传输文件,因为我之前尝试使用 MCSession 的 sendData:toPeers:withMode 确实不可靠。不幸的是,我得到的传输速度非常慢,大约 100kb/s,这不适用于我正在处理的应用程序。这是我的输入和输出流委托方法,这是文件传输发生的地方。

输出流(在流的委托中)

...//previous code
 case NSStreamEventHasSpaceAvailable: {
            //we will only open the stream when we want to send the file.

            NSLog(@"Stream has space available");
            //[self updateStatus:@"Sending"];

            // If we don't have any data buffered, go read the next chunk of data.

            if (totalBytesWritten< outgoingDataBuffer.length) {
                //more stuff to read
                int towrite;
                int diff = outgoingDataBuffer.length-packetSize;
                if (diff <= totalBytesWritten)
                {
                    towrite = outgoingDataBuffer.length - totalBytesWritten;
                } else
                    towrite = packetSize;
                NSRange byteRange = {totalBytesWritten, towrite};
                uint8_t buffer[towrite];
                [outgoingDataBuffer getBytes:buffer range:byteRange];


                NSInteger bytesWritten = [outputStream write:buffer maxLength:towrite];
                totalBytesWritten += bytesWritten;
                NSLog(@"Written %d out of %d bytes",totalBytesWritten, outgoingDataBuffer.length);
            } else {
                //we've written all we can write about the topic?
                NSLog(@"Written %d out of %d bytes",totalBytesWritten, outgoingDataBuffer.length);
                [self endStream];
            }

            // If we're not out of data completely, send the next chunk.
        } break;

输入流

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

    switch(eventCode) {
        case NSStreamEventHasBytesAvailable:
        {
            NSLog(@"Bytes Available");
            //Sent when the input stream has bytes to read, we need to read bytes or else this wont be called again
            //when this happens... we want to read as many bytes as we can

            uint8_t buffer[1024];
            int bytesRead;

            bytesRead = [inputStream read:buffer maxLength:sizeof(buffer)];
            [incomingDataBuffer appendBytes:&buffer length:bytesRead];
            totalBytesRead += bytesRead;
            NSLog(@"Read %d bytes, total read bytes: %d",bytesRead, totalBytesRead);

        }break;
        case NSStreamEventEndEncountered:
        {
            UIImage *newImage = [[UIImage alloc]initWithData:incomingDataBuffer];
            [[self.detailViewController imageView] setImage:newImage];
            NSLog(@"End Encountered");

            [self closeStream];
            //this should get called when there aren't any more bytes being sent down the stream
        }
    }
 }

有没有办法通过多线程或使用稍微修改的使用异步套接字的 NSStream 子类来加速此文件传输?

【问题讨论】:

  • 您还在浏览同行吗?这篇文章 (mzsanford.com/blog/ios7-multipeer-wifi-slow/index.html) 讨论了浏览/广告如何影响您的整体 wifi 性能。另外,你有这个从设备到模拟器的工作吗?我可以从 Sim 转到 Device,但是当我尝试向 Sim 发送数据时,我的输入流会尝试读取一次数据,但它发现零数据然后断开连接。
  • 嗨,布赖恩。我的设置包括两个 ipad mini 运行单独的应用程序执行文件传输。在浏览器和广告商连接后,我仍在浏览对等点,因为当我停止浏览对等点时,我失去了与设备的连接。在使用 sendData:toPeers 方法时,我遇到了频繁断开连接的类似问题。这就是我改用 Stream 方法的原因。
  • 这很奇怪,因为从设备到设备的 Air Drop 运行速度非常快,但执行文件传输真的很慢。 Apple 必须为自己的实现做一些幕后魔术。
  • 你有没有运气?这个新 API 的带宽也低于预期。我正在使用 WIFI ad-hoc 模式。 AirDrop 和 WIFI 网络共享都比我在我的应用程序中看到的要快...
  • 也看到了同样的问题。传输速度非常慢。似乎是多对等框架中的错误。

标签: ios7 file-transfer multipeer-connectivity


【解决方案1】:

我注意到有几件事会导致文件传输缓慢:

我在 MAC 上的 iPhone 5S、iPad Air 和 iPhone 模拟器之间获得了不错的速度(我指的是 500K/秒)。

【讨论】:

    【解决方案2】:

    我有一段时间的传输速率相当不错,突然间我似乎每 15 到 30 秒收到一个数据包。在发送方看起来一切都很好,但数据包会流入接收方。我在我的应用中使用 sendData:toPeers:withMode。

    我的一个设备关闭了 WiFi。即使两个设备都没有连接到 wifi 网络,重新打开它也会恢复我的性能。

    所以我只使用 MPC 进行点对点 ad-hoc 连接,但仍然没有涉及 WiFi 集线器,iOS 似乎正在利用 WiFi 信号来传输我的数据。)我做到了不久前在 Apple 的演示文稿中看到,通过直接蓝牙的 MPC 是一条狗,我可以告诉你一个事实。

    【讨论】:

      【解决方案3】:

      MPC 在 WiFi 或蓝牙上工作,因此这取决于您使用的是什么。如果您使用 wifi 发送文件,则根据网络强度,您将获得最大速度。

      【讨论】:

        猜你喜欢
        • 2016-01-24
        • 2015-07-18
        • 1970-01-01
        • 2016-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多