【问题标题】:How to continuously send udp packets by use GCDAsyncUdpSocket如何使用GCDAsyncUdpSocket连续发送udp数据包
【发布时间】:2024-05-01 14:50:02
【问题描述】:

我使用 GCDAsyncSocket 在我的应用程序中发送图像。图像很大,因此我将其数据拆分为许多小数据包,这些数据包保存在名为 sortNSMutablearray 中。

GCDAsyncUdpSocket *sendSocket;
sendSocket = [[GCDAsycUdpSocket alloc] initwithDelegate:self delegateQueue:dispatch_get_main_queue()];
for(int i = 0;i < sort.count; i++)
{ 
   [sendSocket sendData:[sort objectAtIndex:i toHost:@"239.1.1.110" port:46110 Timeout:-1 tag:1];
}

但是每次发送的数据包距离太近,大部分数据包都会丢失。为了解决这个问题,我添加了如下一行代码:

GCDAsyncUdpSocket *sendSocket;
sendSocket = [[GCDAsycUdpSocket alloc] initwithDelegate:self delegateQueue:dispatch_get_main_queue()];
for(int i = 0;i < sort.count; i++)
{ 
   [sendSocket sendData:[sort objectAtIndex:i toHost:@"239.1.1.110" port:46110 Timeout:-1 tag:1];
   [Thread sleepForTimeInterval:0.003f];
}

因此,它使我的应用程序变得很奇怪。所以我想知道任何其他方法来为每次发送添加 timeInterval。感谢任何帮助,提前非常感谢。

【问题讨论】:

    标签: objective-c sockets cocoa udp gcdasyncudpsocket


    【解决方案1】:

    在 GCDAsyncudpSocket.m 中使用此代码

                if (socketError.code == ENOBUFS) {
                    [self notifyDidNotSendDataWithTag:currentSend->tag   dueToError:[self errnoErrorWithReason:@"Buffer Full"]];
                    [self endCurrentSend];
                    [self maybeDequeueSend];
                    usleep(10000);
                }
    

    【讨论】: