C#中的UDP“连接"-速度 [英] UDP "Connect"-Speed in C#

查看:60
本文介绍了C#中的UDP“连接"-速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我分析了一些样板UDP代码,并且速度非常快,仅发送少量数据(这是我的意图).

I profiled some boilerplate UDP code and the speed was very good for sending only some small amount data (which is my intent).

但是,与发送"方法相比,连接"方法非常慢".这需要50到80毫秒: udpClient = new UdpClient(); udpClient.Connect("HOSTNAME",11000);

BUT the "connect" method is "very slow" compared to the "send" method. This takes 50 - 80 ms: udpClient = new UdpClient(); udpClient.Connect("HOSTNAME", 11000);

发送几乎没有1 ms的时间,因为它是如此之快: Byte [] sendBytes = Encoding.ASCII.GetBytes(有人在吗?");

Sending is then hardly profiled by 1 ms, because its so amazing fast: Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");

udpClient.Send(sendBytes,sendBytes.Length);

我想知道这种连接"方法的作用,因为UDP在设计上是无连接的.

I wonder what this "connect" method does, since UDP is connectionless by design.

如果我忽略了connect方法,则每次呼叫发送速度会变慢: udpClient.Send(sendBytes,sendBytes.Length,"HOSTNAME",1100);

If I leave out the connect method, then send is slower per call: udpClient.Send(sendBytes, sendBytes.Length,"HOSTNAME",1100);

有没有机会提高连接"速度?

Any chance to improve the "connect" speed?

免责声明:我知道UDP是不可靠的,但是对于我的应用程序(客户端统计信息,无论如何也不是100%准确的),软件包顺序错误甚至丢失的软件包都不会杀死我都没有关系./p>

Disclaimer: I know UDP is unreliable, but for my app (client statistics, which are not 100% exact anyway) it doesn't matter if packages come in wrong order and even some lost packages don't kill me.

推荐答案

udpClient.Connect设置默认的主机名和端口,因此随后您可以调用Send而不指定它们.花时间进行名称解析-将HOSTNAME转换为IPAddress.

udpClient.Connect sets the default host name and port, so subsequently you can call Send without specifying them. What's taking the time is the name resolution - translating HOSTNAME into an IPAddress.

如果您在Connect中执行一次,则不必每次发送时都这样做,这更快.

If you do it once in Connect, you don't have to do it every time you Send, which is faster.

这篇关于C#中的UDP“连接"-速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆