本文介绍了通过UDP从服务器向iPhone发送大量实时处理的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施一个远程应用程序。服务器将处理&作为动画实时渲染数据。 (准确地说是一系列图像)每次渲染图像时,它都会通过UDP传输到接收的iPhone客户端。

I'm implementing a remote application. The server will process & render data in real time as animation. (a series of images, to be precise) Each time, an image is rendered, it will be transferred to the receiving iPhone client via UDP.

我研究了一些UDP我知道以下内容:

I have studied some UDP and I am aware of the following:


  • UDP的最大大小约为65k。

  • UDP has max size of about 65k.

但是,似乎iPhone只能接收41k的UDP数据包。 iPhone似乎无法接收大于此数据包。

However, it seems that iPhone can only receive 41k UDP packet. iPhone seems to not be able to receive packet larger than that.

发送多个数据包时,会丢弃许多数据包。这是由于UDP处理过大。

When sending multiple packets, many packets are being dropped. This is due to oversizing UDP processing.

减少数据包大小会增加未丢弃的数据包数量,但这意味着需要发送更多数据包。

Reducing packet size increase the amount of packets not being dropped, but this means more packets are required to be sent.

之前我从未编写过真正的实用UDP应用程序,所以我需要一些有效的UDP通信指导。在这种情况下,我们正在讨论从服务器实时传输渲染图像以在iPhone上显示。

I never write real practical UDP applications before, so I need some guidance for efficient UDP communication. In this case, we are talking about transferring rendered images in real time from the server to be displayed on iPhone.

压缩数据似乎是强制性的,但在这个问题中,我想关注UDP部分。通常,当我们实现UDP应用程序时,如果我们需要实时不间断地发送大量数据,我们可以在高效UDP编程的最佳实践方面做些什么呢?

Compressing data seems mandatory, but in this question, I would like to focus on the UDP part. Normally, when we implement UDP applications, what can we do in terms of best practice for efficient UDP programming if we need to send a lot of data non-stop in real time?

推荐答案

假设您有一个非常具体和充分的理由使用UDP并且您需要所有数据到达(即您不能容忍任何丢失数据)然后你需要做一些事情(这假定一个单播应用程序):

Assuming that you have a very specific and good reason for using UDP and that you need all your data to arrive ( i.e. you can't tolerate any lost data ) then there are a few things you need to do ( this assumes a uni-cast application ):


  1. 添加序列号到每个数据包的标题

  2. 确认每个数据包

  3. 设置重新传输计时器,如果没有ack recv'ed重新发送数据包

  4. 跟踪延迟RTT(往返时间),以便您知道设置计时器的时间长度

  5. 如果这对您的应用程序很重要,可能会处理无序数据到达

  6. 增加客户端套接字上的接收缓冲区大小

  1. Add a sequence number to the header for each packet
  2. Ack each packet
  3. Set up a retransmit timer which resends the packet if no ack recv'ed
  4. Track latency RTT ( round trip time ) so you know how long to set your timers for
  5. Potentially deal with out of order data arrival if that's important to your app
  6. Increase receive buffer size on client socket.

此外,你可能是森ding如此之快,以至于你在发送机器内部丢弃数据包,甚至没有将网卡放到线路上。在某些系统上,在发送套接字上调用select-write-ablity可以帮助解决这个问题。此外,在UDP套接字上调用connect可以加快性能,从而减少数据包丢失。

Also, you could be sending so fast that you are dropping packets internally on the sending machine without them even getting out the NIC onto the wire. On certain systems calling select for write-ablity on the sending socket can help with this. Also, calling connect on the UDP socket can speed up performance leading to less dropped packets.

基本上,如果您需要保证按顺序交付数据而不是在UDP之上重新实现TCP。如果您使用UDP的唯一原因是延迟,那么您可以使用TCP并禁用。如果您希望打包数据具有可靠的低延迟交付,另一种可能性是,同时禁用Nagle。它还可以提供无序传送以加快速度。

Basically, if you need guaranteed in-order delivery of your data than you are going to re-implement TCP on top of UDP. If the only reason you use UDP is latency, then you can probably use TCP and disable the Nagle Algorithm. If you want packetized data with reliable low latency delivery another possibility is SCTP, also with Nagle disabled. It can also provide out-of-order delivery to speed things up even more.

我会推荐史蒂文的它有一个关于高级UDP的部分以及何时适合使用UDP而不是TCP。作为一个说明,他建议不要使用UDP进行批量数据传输,尽管现实情况是,这对于流式多媒体应用来说正变得越来越普遍。

I would recommend Steven's "Unix Network Programming" which has a section on advanced UDP and when it's appropriate to use UDP instead of TCP. As a note, he recommends against using UDP for bulk data transfer, although the reality is that this is becoming much more common these days for streaming multimedia apps.

这篇关于通过UDP从服务器向iPhone发送大量实时处理的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 01:45