简体   繁体   English

在同一个应用程序中使用TCP和UDP的任何问题?

[英]Any problems with using TCP and UDP in the same application?

The point of my question is to ask if it is accepted to use both TCP and UDP to communicate between client and server. 我的问题是询问是否接受使用TCP和UDP在客户端和服务器之间进行通信。

I am making a real-time client server game with parts of the communication that need to be guaranteed (logging in, etc..), but other parts will be ok to lose packets (state updates, etc). 我正在制作一个实时客户端服务器游戏,其中包含需要保证的部分通信(登录等),但其他部分可以丢失数据包(状态更新等)。 So, I would like to use UDP for most of the data communication but I do not want to have to implement my own framework to insure that my control communication (logging in) is guaranteed. 所以,我想在大多数数据通信中使用UDP,但我不想实现自己的框架,以确保我的控制通信(登录)得到保证。

So, would it be reasonable to initially use TCP to manage a connection, and then on a separate port send data communication pack and forth? 那么,最初使用TCP来管理连接,然后在一个单独的端口上发送数据通信包是否合理?

You should absolutely do it that way (use TCP and UDP to accomplish different communication tasks.) And you don't even have to use two different ports. 你绝对应该这样做(使用TCP和UDP来完成不同的通信任务。)你甚至不必使用两个不同的端口。 One will suffice . 一个就足够了 You can listen to the two different protocols on the same port. 您可以在同一端口上侦听两种不同的协议。

It is quite reasonable and already used in mainstream. 它非常合理,已经在主流中使用。 Even when browsing the Web, DNS operations are UDP-based and HTTP connections are TCP-based. 即使在浏览Web时,DNS操作也是基于UDP的,HTTP连接是基于TCP的。

Keep in mind that you should either consider the two connection types to be completely independent or employ additional measures to properly handle any inter-dependencies. 请记住,您应该将两种连接类型视为完全独立,或采用其他措施来正确处理任何相互依赖关系。 TCP connections can have timing issues at the OS and network levels and UDP connections have packet loss issues. TCP连接可能会在操作系统和网络级别出现计时问题,并且UDP连接会出现数据包丢失问题。 You should take specific measures to avoid deadlocks and performance problems when the TCP part of your application stalls or a UDP packet is lost. 当应用程序的TCP部分停止或UDP数据包丢失时,您应采取特定措施以避免死锁和性能问题。

It is not only accepted but is widely used. 它不仅被接受,而且被广泛使用。 As a good example, BATS Exchange is using this approach in their market data distribution system, to implement a recovery mechanisms. 举个好例子, BATS Exchange在其市场数据分配系统中使用这种方法来实现恢复机制。

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

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