简体   繁体   中英

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. 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. 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.

There are other good reasons for using different udp sockets to receive and send, related to how the application protocol design and to reliability.

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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