0

我使用 GCDAsyncSocket 在我的应用程序中发送图像。图像很大,所以我将其数据拆分为许多小数据包,这些数据包保存在一个NSMutablearray被调用的sort.

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 的任何其他方法。感谢任何帮助,提前非常感谢。

4

1 回答 1

0

在 GCDAsyncudpSocket.m 中使用此代码

            if (socketError.code == ENOBUFS) {
                [self notifyDidNotSendDataWithTag:currentSend->tag   dueToError:[self errnoErrorWithReason:@"Buffer Full"]];
                [self endCurrentSend];
                [self maybeDequeueSend];
                usleep(10000);
            }
于 2016-11-24T09:18:12.597 回答