3

I'm developing an application in Rust in which I need to transfer two types of data to a server on the Internet:

  • Lots of "less important" information, for which I would use the UDP protocol.
  • Little "very important" information that I would use the TCP protocol for.

Is it possible to use both protocols simultaneously? Or is there any protocol (available in Rust) that allows me to send both types of information without major consequences?

5
  • of course it's possible but not an the same port. "Is this more correct?" it's really depend, it's a pattern used a lot in (well coded) server fps game. "Is there any protocol (available in rust) that allows me to send both types of information without major consequences?" would not make anysense. Commented Dec 17, 2021 at 19:16
  • that said, it's hard enough to code a server in TCP, it's hard to code a server in UDP, and so code both that need to "work together" is very hard Commented Dec 17, 2021 at 19:20
  • 3
    @Stargateur: "of course it's possible but not an the same port" - UDP and TCP each have their own port "space", i.e. it is perfectly possible to use the same port number of UDP and TCP. It is even common: DNS does this with port 53, UDP based HTTP/3 and TCP based HTTP both use port 443. Commented Dec 17, 2021 at 20:24
  • UDP vs. TCP is not that much about "less important" vs. "very important". UDP is about messages where message loss, duplication or reordering is acceptable but on-time delivery is important, i.e. things like real time audio. TCP is about reliable in-order delivery at all costs, even if if leads to higher latency. Commented Dec 17, 2021 at 20:30
  • i would formulate the message of Steffen Ullrich as: UDP is a good idea when the mechanisms of TCP (in particular the way it achieves in-order delivery, also known as head-of-line blocking and congestion control) are not an option. Usually you have another protocol on top of UDP that achieves similar things in a way that is acceptable for your data. Commented Dec 18, 2021 at 12:25

1 Answer 1

1

you can always use both together listening on different ports of course. Your question has little to do with rust more to do with networking or communication design in general I believe.

I also developed a similar project using both protocols.

In my project, I wrote a socks5 multiplexer. Basically, the user connects to my server using socks5 protocol on TCP using any socs5 compatible client then I distribute this over UDP to my workers and they connect to remote over TCP again because that's how HTTP works.

In my case, the reason I chose UDP for communication among primary and secondaries is that UDP is a frame-based protocol and that enabled me to implement my protocol much easier since I did not need to follow where a message starts and ends simply when I sent a frame of length n from one node to another I would read n bytes on the other hand. That's not the case with TCP. In TCP the overall content integrity is guaranteed however there are no guarantees on particular frame size. For instance, you can write 50 bytes on one end and receive 20 bytes and 30 bytes on the other node.

So based on your needs you should choose the correct protocol also understand the ups and downs of each protocol.

To summarize

TCP: Good for overall data integrity but really bad if you have a frame-based protocol. You'd need your own framing logic.

UDP: Amazing if you are developing a frame-based protocol however some frames might just disappear sometimes no guarantees on the overall integrity.

Sign up to request clarification or add additional context in comments.

3 Comments

you do not need tcp and udp listening on different ports. All sockets are matched based on <scr ip, dst ip, src port, dst port, *proto*> tuple, where proto is transport protocol.
and using UDP instead of properly defining message boundary in a stream is a particularly bad reason for using UDP.
Framing logic for TCP is not that difficult. e.g. you can just add a 4-byte uint32 size-header before each frame, and add logic to the receiver to parse that to know how many bytes to read in for the next frame, and repeat.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.