UDP数据传输比TCP慢数据传输、UDP、TCP

由网友(选择性死亡)分享简介:我目前正在写在C#/。NET4原型应用程序中,我需要传递一个未知量的数据。将数据从一个文本文件中读入,然后序列化为一个字节数组。现在我需要实现这两种传输方式,UDP和TCP。这两种方式的传输不工作的罚款,但我有UDP一些struggleing。我assumend,使用UDP的传输必须比使用TCP快得多但实际上我的试验...

我目前正在写在C#/。NET4原型应用程序中,我需要传递一个未知量的数据。将数据从一个文本文件中读入,然后序列化为一个字节数组。 现在我需要实现这两种传输方式,UDP和TCP。这两种方式的传输不工作的罚款,但我有UDP一些struggleing。我assumend,使用UDP的传输必须比使用TCP快得多但实际上我的试验证明,UDP传输比使用TCP慢约7至8倍。 我测试了传输带12兆字节的文件和TCP传输花了约1秒,而UDP传输花了大约7秒。 在应用中,我使用简单套接字发送数据。由于UDP并只允许最多每消息65535kb,我劈裂文件的序列化的字节数组分成几个部分,每个部分有多功能转换插座SendBufferSize的大小,然后我转用Socket.Send()方法调用每个部分。

I'm currently writing a prototype application in C#/.Net4 where i need to transfer an unknown amount of data. The data is read in from a text file and then serialized into a byte array. Now i need to implement both transmission methods, UDP and TCP. The transmission in both ways does work fine but i have some struggleing with UDP. I assumend that the transmission using UDP have to be much faster than using TCP but in fact my tests proved that the UDP transmission is about 7 to 8 times slower than using TCP. I tested the transmission with a 12 megabyte file and the TCP transmission took about 1 second whereas the UDP transmission took about 7 seconds. In the application i use simple sockets to transmit the data. Since UDP does only allow a maximum of 65535kb per message, i splitted the serialized the byte array of the file into several parts where each part has the size of the socker SendBufferSize and then i transfer each part using Socket.Send() method call.

下面是code为发件人的部分。

Here is the code for the Sender part.

while (startOffset < data.Length)
{
    if ((startOffset + payloadSize) > data.Length)
    {
        payloadSize = data.Length - startOffset;
    }
    byte[] subMessageBytes = new byte[payloadSize + 16];
    byte[] messagePrefix = new UdpMessagePrefix(data.Length, payloadSize, messageCount, messageId).ToByteArray();
    Buffer.BlockCopy(messagePrefix, 0, subMessageBytes, 0, 16);
    Buffer.BlockCopy(data, startOffset, subMessageBytes, messageOffset, payloadSize);
    messageId++;
    startOffset += payloadSize;
    udpClient.Send(subMessageBytes, subMessageBytes.Length);
    messages.Add(subMessageBytes);
}

这code没有简单地复制下一部分被发送到一个字节数组,然后调用插槽上的发送方法。我的第一个猜测是,该字节数组的分裂/复印了放缓的表现,但我隔离并测试了分裂code和分裂只花了几毫秒,所以这是不是造成问题的原因。

This code does simply copy the next part to be send into an byte array and then call the send method on the socket. My first guess was, that the splitting/copying of the byte arrays was slowing down the performance, but i isolated and tested the splitting code and the splitting took only a few milliseconds, so that was not causing the problem.

int receivedMessageCount = 1;
Dictionary<int, byte[]> receivedMessages = new Dictionary<int, byte[]>();
while (receivedMessageCount != totalMessageCount)
{
    byte[] data = udpClient.Receive(ref remoteIpEndPoint);
    UdpMessagePrefix p = UdpMessagePrefix.FromByteArray(data);
    receivedMessages.Add(p.MessageId, data);
    //Console.WriteLine("Received packet: " + receivedMessageCount + " (ID: " + p.MessageId + ")");
    receivedMessageCount++;
    //Console.WriteLine("ReceivedMessageCount: " + receivedMessageCount);
}
Console.WriteLine("Done...");
return receivedMessages;

这是在服务器端code其中我收到UDP报文。每个消息有一些字节作为一个preFIX其中消息的总数被存储和大小。所以我只需拨打socket.Receive在一个循环,直到我收到的消息这是在preFIX规定的金额。

This is the server side code where i receive the UDP messages. Each message has some bytes as a prefix where the total number of messages is stored and the size. So i simply call socket.Receive in a loop until i received the amount of messages which were specified in the prefix.

我在这里的假设是,我可能已经实现了UDP传输code不是有效够了......也许有你们已经看到了问题,在code段或有其他任何建议或提示我为什么我的UDP传输比TCP慢。

My assumption here is that i may have implemented the UDP transmission code not "efficiently" enough... Maybe one of you guys already sees a problem in the code snippets or have any other suggestion or hint for me why my UDP transmission is slower than TCP.

在此先感谢!

推荐答案

而UDP数据包的大小可以达到64K,实际的线框通常是1500字节(普通以太网的 MTU )。还具有以适合至少20个字节和8个字节的UDP头的IP报头,留给你1472字节可用的有效负载的

While UDP datagram size can be up to 64K, the actual wire frames are usually 1500 bytes (normal ethernet MTU). That also has to fit an IP header of minimum 20 bytes and a UDP header of 8 bytes, leaving you with 1472 bytes of usable payload.

你们看到的是你的操作系统的网络协议栈的碎片的在发送器侧的UDP数据报,然后重新组装的它们在接收器侧。这需要时间,因此你的结果。

What you are seeing is the result of your OS network stack fragmenting the UDP datagrams on the sender side and then re-assembling them on the receiver side. That takes time, thus your results.

的TCP,另一方面,执行自身分组,并试图找到路径MTU 时,因此它在这种情况下,更有效。

TCP, on the other hand, does its own packetization and tries to find path MTU, so it's more efficient in this case.

限制你的数据块为1472字节,并重新测量。

Limit your data chunks to 1472 bytes and measure again.

阅读全文

相关推荐

最新文章