通过 bonjour 发送大数据

发布于 2024-10-31 18:59:34 字数 1294 浏览 2 评论 0原文

我目前有一种通过 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 技术交流群。

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

发布评论

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

评论(1

旧人哭 2024-11-07 18:59:34

您不是通过“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 ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文