Ios7 使用多EER连接框架的慢速文件传输
我正在使用iOS 7的Multipeer连接框架在两台设备之间发送一个文件。我正在使用NSStreams传输文件,因为我以前尝试使用MCSession的sendData:toPeers:withMode时确实不可靠。不幸的是,我得到的传输速度非常慢,大约为100kb/s,这对我正在使用的应用程序不起作用。下面是我的输入和输出流委托方法,这是文件传输发生的地方 输出流(在流的委托中)Ios7 使用多EER连接框架的慢速文件传输,ios7,file-transfer,multipeer-connectivity,Ios7,File Transfer,Multipeer Connectivity,我正在使用iOS 7的Multipeer连接框架在两台设备之间发送一个文件。我正在使用NSStreams传输文件,因为我以前尝试使用MCSession的sendData:toPeers:withMode时确实不可靠。不幸的是,我得到的传输速度非常慢,大约为100kb/s,这对我正在使用的应用程序不起作用。下面是我的输入和输出流委托方法,这是文件传输发生的地方 输出流(在流的委托中) 有没有一种方法可以通过多线程或使用稍微修改的NSStream子类(使用异步套接字)来加快文件传输速度?我注意到有两
有没有一种方法可以通过多线程或使用稍微修改的NSStream子类(使用异步套接字)来加快文件传输速度?我注意到有两件事情会导致文件传输速度变慢:
- 会话中有非空投兼容设备()
- WiFi问题(饱和网络、路由器问题、通道等)
在MAC上的iPhone 5S、iPad Air和iPhone模拟器之间,我的速度一直不错(我指的是500K/秒)。有一段时间我的传输速度相当不错,突然间,我似乎每15到30秒就收到一个数据包。发送端看起来都很好,但数据包会慢慢地进入接收方。我在应用程序中使用sendData:toPeers:withMode 我的一台设备关闭了WiFi。即使两台设备均未连接到wifi网络,但重新启用该功能可恢复我的性能
所以我只使用MPC进行点对点的临时连接,但仍然没有涉及WiFi集线器,iOS似乎正在利用WiFi信号来传输我的数据。)不久前,我确实在苹果公司的一次演示中看到,直接蓝牙上的MPC是一只狗,我可以告诉你一个事实,它是一只狗。MPC在WiFi或蓝牙上工作,所以它取决于你使用的是什么。如果您使用wifi发送文件,则根据网络强度,您将获得最大速度。您是否仍在浏览对等站点?本文()讨论浏览/广告如何影响您的整体wifi性能。另外,你有这个工作从设备到模拟器?我可以从一个Sim卡切换到另一个设备,但当我尝试发送到Sim卡时,我的输入流尝试读取一次数据,但它发现零数据,然后断开连接。嘿,布莱恩。我的设置由两个运行不同应用程序的iPadMini组成,它们执行文件传输。在浏览器和广告商连接后,我仍在浏览对等方,因为当我停止浏览对等方时,我将失去与设备的连接。我在使用sendData:toPeers方法时遇到了类似的频繁断开连接的问题。这就是为什么我转而使用流方法。这很奇怪,因为从一个设备到另一个设备的空投速度很快,但执行文件传输却非常慢。苹果一定在为自己的实现做一些幕后魔术。你在这方面运气好吗?使用此新API时,我还遇到了低于预期的带宽问题。我正在使用WIFI临时模式。空投和WIFI连接都比我在应用程序中看到的要快得多……我也看到了同样的问题。传输速度非常慢。似乎是多对等框架中的一个bug。
...//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
}
}
}