原始 Socket 和 UdpClient 之间的性能(或其他)差异?
正如我们所知,.Net 有 UdpClient 用于 UDP 的简单套接字使用。
来自 MSDN:
如果您正在编写相对简单的应用程序并且不需要最大性能,请考虑使用 TcpClient、TcpListener 和 UdpClient。这些类为 Socket 通信提供了更简单、更用户友好的接口。
我想知道 Raw Socket 和 UdpClient 之间的性能差异有多大?我知道 UdpClient 是 Udp 套接字的包装器,它没有异步读/写功能。
还要别的吗?
谢谢
As we know .Net has UdpClient for simple Socket usage for UDP.
From MSDN:
If you are writing a relatively simple application and do not require maximum performance, consider using TcpClient, TcpListener, and UdpClient. These classes provide a simpler and more user-friendly interface to Socket communications.
I am wondering how much performance differences between Raw Socket and UdpClient? I know UdpClient is a wrapper of socket for Udp and it does not have asynchron read/write.
Anything else?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如文档所说,UdpClient/TcpClient 是 Socket 类之上的一个薄包装器。如果您只想发送/接收数据块,那么这些类就很好。对于这种场景,Socket 和 UdpClient/TcpClient 之间的性能没有差异。
然而,Socket 确实以 XXXAsync() 方法的形式提供了一种更快的 IO 方法。这些方法允许您执行非常快的 I/O,并且不会在 TcpClient/UdpClient 中公开。这就是文档中“性能差异”的含义 - 为了更快的性能,您必须深入研究 Socket 类并使用这些方法(XXXAsync)。
As the docs say, UdpClient/TcpClient are a thin wrapper on top of Socket class. If all you want to do is send/receive blobs of data, then these classes are good. For this scenario, there is no difference in performance between Socket and UdpClient/TcpClient.
However, Socket does provide a faster way to do IO, in the form of XXXAsync() methods. These methods allow you to do very fast I/O, and are not exposed in TcpClient/UdpClient. That is what the docs mean by "perf difference" - it is that for faster perf, you will have to dig into Socket class and use these methods (XXXAsync).
sendfile 并不快。
浪费了我几个小时的宝贵时间。
sendfile is not faster.
wasted couple hours of my precious time.
UDP是一种无连接协议,具有零错误检查,它是与TCP的权衡,它比TCP更快。与 TCP 相比,UDP 是一种即发即忘协议,其中完整性和校验和是 TCP 相对于 UDP 的额外开销。
无论使用哪种协议,RawSocket 都没有任何影响或差异。 RawSocket 就是这样,原始的,你必须放入相关的标头,例如源/目标 IP 地址,并确保标头符合相关协议。它与套接字的速度无关 - 这当然取决于您选择的协议。
希望这有帮助,
此致,
汤姆.
UDP is a connectionless protocol which has zero error-checking, it is that, is the trade-off with TCP, it is faster than TCP. Think of UDP as a fire-and-forget protocol in comparison to TCP, where integrity and checksum is the additional overhead of the TCP over UDP.
RawSocket has no bearing or differences regardless of the protocol in use. A RawSocket is just that, raw, you have to put in the relevant headers such as the source/destination IP address, and to ensure that the headers conform to the relevant protocol. It has nothing to do with the speed of the socket - that will of course depend on the protocol you choose.
Hope this helps,
Best regards,
Tom.