简体   繁体   English

使用专用 DatagramSockets 发送和接收 UDP 数据报是否有(性能)优势?

[英]Is there a (performance) benefit in having dedicated DatagramSockets for sending and receiving UDP datagrams?

I have some datagram sockets, one is for input and other for output with different ports.我有一些数据报 sockets,一个用于输入,另一个用于具有不同端口的 output。 Is there any reason for doing it?有什么理由这样做吗? Is for best performance?是为了最好的表现吗?

| <- Receiver, accept and accumulate some packets received from client
| <-> Proccess, request packets from Receiver, process and put some response into Sender
| -> Sender, just immediate send

I am thinking about combine Receiver and Sender and use one socket.我正在考虑结合接收器和发送器并使用一个套接字。 Receiver and Sender are some modules. Receiver 和 Sender 是一些模块。 They may be initialized in other part of the programm (for example in communication between other modules over the web).它们可以在程序的其他部分初始化(例如在其他模块之间通过网络进行的通信)。

There is no performance benefit, in the sense that data does not travel any faster or in greater quantities with two sockets than with one.没有性能优势,因为使用两个 sockets 的数据不会比使用一个更快或更多。

There are other good reasons for using different udp sockets to receive and send, related to how the application protocol design and to reliability.使用不同的 udp sockets 进行接收和发送还有其他充分的理由,这与应用协议的设计方式和可靠性有关。

Each socket has send and receive buffers of its own.每个套接字都有自己的发送和接收缓冲区。 If either of these buffers becomes full, packets will be dropped.如果这些缓冲区中的任何一个变满,数据包将被丢弃。 In other words, using a single socket for multiple purposes may increase the risk of packet drops.换句话说,将单个套接字用于多种用途可能会增加丢包的风险。

Also, with different sockets you can use different local ports.此外,对于不同的 sockets,您可以使用不同的本地端口。 A benefit from different local ports is it gives you an easy way to handle separate streams of packets for different clients.不同本地端口的一个好处是它为您提供了一种为不同客户端处理单独数据包流的简单方法。 You can see an example of this in the design of the TFTP protocol https://en.m.wikipedia.org/wiki/Trivial_File_Transfer_Protocol您可以在 TFTP 协议https://en.m.wikipedia.org/wiki/Trivial_File_Transfer_Protocol的设计中看到一个这样的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM