使用Multipeer Connectivity框架进行慢速文件传输

时间:2013-10-10 21:08:35

标签: ios7 file-transfer multipeer-connectivity

我正在使用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子类来加速这种文件传输?

3 个答案:

答案 0 :(得分:3)

我注意到有几件事导致文件传输速度慢:

我已经在MAC 5S,iPad Air和iPhone模拟器之间获得了不错的速度(而且我的意思是500K /秒)。

答案 1 :(得分:1)

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

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

所以我只与MPC进行点对点ad-hoc连接,但是,由于没有涉及WiFi集线器,似乎iOS正在利用WiFi信号来传输我的数据。)我确实在Apple的一个演示文稿中看到,直接BlueTooth的MPC是一只狗,我可以告诉你一个事实,它是。

答案 2 :(得分:1)

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