对于网络来说,Java 有什么更好的选择? UDP还是TCP?
java中的mmo游戏应该使用什么。 UDP还是TCP?为什么? TCP 是点对点关系,可以传送每个数据包,而 UDP 没有点对点关系,可能会丢弃数据包,从而导致延迟。在这种情况下使用哪一个更好?
What should be used for a mmo game in java. UDP or TCP? And why? TCP is a point-to-point relationship and carries every packet through while UDP has no point-to-point relationship and can drop off packets resulting in lag. Which one is the better one to use in this scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
答案不取决于语言,而是取决于游戏的要求。
如果您的游戏可以处理玩家(或服务器)丢失或无序到达的状态更新,那么 UDP 应该没问题。
如果您需要最小延迟的实时响应(并且上述问题已解决),那么您还应该使用 UDP。
否则,您应该使用 TCP。
The answer depends not on the language, but on the requirements of the game.
If your game can cope with status updates from players (or the server) either going missing, or arriving out of order, then UDP should be fine.
If you need real-time response with minimal latency (and the above issues are solved) then you should also UDP.
Otherwise, you should use TCP.
不管是否是 Java,TCP 和 UDP 都具有相同的优点和缺点,且与语言无关。
但更多时候,它可以归结为一个基本的设计问题:当数据包丢失时,会发生什么?
这两种方法本身都没有对错,都会给你的游戏带来问题,但这是你应该首先回答的问题。
It doesn't matter if it's Java or not, TCP and UDP have the same advantages and drawbacks that are independent of the language.
But more often than not it boils down to one basic design question: when a packet is dropped, what should happen?
Neither approach is right or wrong in itself, both will cause problems in your game, but this is the question you should answer first.
请参阅此问题:
Android 游戏 UDP/TCP?
See this question:
Android game UDP / TCP?
这个问题与 Java 并没有真正的联系。 UDP 数据报不能保证到达目的地,而 TCP 数据报可以。连续的 UDP 数据报也可能无序到达目的地。例如,DNS 基于 UDP,因为请求和响应只需要一个数据报。如果您需要可靠性并且不想实施重试。 TCP是你的选择。如今,计算开销很小,因此我认为使用 UDP 不会带来太多性能提升。
The question is not really linked to Java. UDP datagrams are not guaranteed to reach the destination, TCP datagrams are. Consecutive UDP datagrams can also reach their destinations out of order. For instance DNS is based on UDP because requests and responses just take one datagram. If you need reliability and do not want to implement retries. TCP is your choice. Nowadays, the computing overhead is minimal, so that I don't think there is much performance gain to expect from favouring UDP.
我对 UDP 和 TCP 的体验是这样的:
所以 UDP 对于非必需的东西很有用。例如,如果有 2 个家伙在你的游戏中跑来跑去,并且玩家 A 每 100 毫秒通过 UDP 接收玩家 B 的当前坐标和速度,玩家 A 就可以推断一段时间,而不会偏离太远。另一方面,如果玩家 A 拿到葫芦,而玩家 B 拿到皇家同花顺,情况就不同了。
在我的项目中,我使用 UDP 作为主要通信方案,每个接收器发回通知。如果通信失败的时间超过 X,我就会诉诸 TCP。
My experience with UDP and TCP is like this:
So UDP is good for non-essential stuff. For example, if 2 dudes are running around in your game, and Player A receives Player B's current coordinates and velocity over UDP every 100 millis, Player A can extrapolate for a while without going too far off. If on the other hand Player A has a full house, and Player B got a royal flush, the situation is different.
In my project, I used UDP as the primary communication scheme, with every receiver sending back a notification. If communication failed for longer than X though, I resorted to TCP.