通过 bonjour 发送大数据
我目前有一种通过 Bonjour 发送数据的方法。问题是我的数据限制为 1000 kB。我知道如果我想发送更大的数据,我需要将其分成数据包。
但这提出了一个问题:如何防止数据包丢失,并确保所有数据包都被接收方收到。
我不擅长网络,想请你帮我改变这个简单的方法,以实现更大的数据传输。
- (BOOL)sendData:(NSData *)data error:(NSError **)error {
BOOL successful = NO;
if(self.outputStreamHasSpace) {
NSInteger len = [self.outputStream write:[data bytes] maxLength:[data length]];
if(-1 == len) {
// error occured
*error = [[NSError alloc]
initWithDomain:ServerErrorDomain
code:kServerNoSpaceOnOutputStream
userInfo:[[self.outputStream streamError] userInfo]];
} else if(0 == len) {
// stream has reached capacity
*error = [[NSError alloc]
initWithDomain:ServerErrorDomain
code:kServerOutputStreamReachedCapacity
userInfo:[[self.outputStream streamError] userInfo]];
} else {
successful = YES;
}
} else {
*error = [[NSError alloc] initWithDomain:ServerErrorDomain
code:kServerNoSpaceOnOutputStream
userInfo:nil];
}
return successful;
}
谢谢。
I currently have a method that sends data through Bonjour. The problem is that my data is limited to 1000 kB. I know that if I want to send larger data I need to break it in to packets.
But this raises a question of how do I prevent packets from being lost, and ensure all packets are received by the receiver.
I am not good with network and would like to ask you to help me change this simple method to enable larger data transfer.
- (BOOL)sendData:(NSData *)data error:(NSError **)error {
BOOL successful = NO;
if(self.outputStreamHasSpace) {
NSInteger len = [self.outputStream write:[data bytes] maxLength:[data length]];
if(-1 == len) {
// error occured
*error = [[NSError alloc]
initWithDomain:ServerErrorDomain
code:kServerNoSpaceOnOutputStream
userInfo:[[self.outputStream streamError] userInfo]];
} else if(0 == len) {
// stream has reached capacity
*error = [[NSError alloc]
initWithDomain:ServerErrorDomain
code:kServerOutputStreamReachedCapacity
userInfo:[[self.outputStream streamError] userInfo]];
} else {
successful = YES;
}
} else {
*error = [[NSError alloc] initWithDomain:ServerErrorDomain
code:kServerNoSpaceOnOutputStream
userInfo:nil];
}
return successful;
}
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不是通过“Bonjour”发送它们,而是将 UDP 数据包发送到多播地址。在大多数网络上,最大帧大小为 1500 字节。实际上,考虑到标头、VLAN 标记等,每帧大约需要填充 1.3 - 1.4k 数据。当数据通过 UDP 传输时,控制数据包的正确接收和排序取决于您 - 这是不使用 TCP 的缺点之一;)
You're not sending them over 'Bonjour', you're sending UDP packets to a multicast address. On most networks the maximum frame size is 1500 bytes. Realistically, allowing for headers, vlan tags, etc, you have about 1.3 - 1.4k of data per frame to fill. As the data's going over UDP, controlling the correct reception and ordering of packets is up to you- it's one of the drawbacks of not using TCP ;)