对于网络来说,Java 有什么更好的选择? UDP还是TCP?

发布于 2024-10-16 07:48:37 字数 105 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吾性傲以野 2024-10-23 07:48:38

答案不取决于语言,而是取决于游戏的要求。

如果您的游戏可以处理玩家(或服务器)丢失或无序到达的状态更新,那么 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.

莫多说 2024-10-23 07:48:38

不管是否是 Java,TCP 和 UDP 都具有相同的优点和缺点,且与语言无关。

但更多时候,它可以归结为一个基本的设计问题:当数据包丢失时,会发生什么?

  1. 假装这件事从未发生过。这是UDP的领地。
  2. 等待数据包重新发送。这就是应该使用 TCP 的地方。

这两种方法本身都没有对错,都会给你的游戏带来问题,但这是你应该首先回答的问题。

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?

  1. Pretend it never happened. This is UDP territory.
  2. Wait until the packet is resent. This is where TCP should be used.

Neither approach is right or wrong in itself, both will cause problems in your game, but this is the question you should answer first.

很快妥协 2024-10-23 07:48:38

这个问题与 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.

陌路终见情 2024-10-23 07:48:38

我对 UDP 和 TCP 的体验是这样的:

  • UDP 显着更快。我们说的是两个数量级。
  • 在有线网络上,UDP 数据包丢失率低于 1%
  • 在无线网络上,UDP 数据包丢失率很容易达到 80%,并且与路由器的物理距离很重要:一英尺外您将丢失 20% 的数据包,20 米外您将丢失 50%英尺远。

所以 UDP 对于非必需的东西很有用。例如,如果有 2 个家伙在你的游戏中跑来跑去,并且玩家 A 每 100 毫秒通过 UDP 接收玩家 B 的当前坐标和速度,玩家 A 就可以推断一段时间,而不会偏离太远。另一方面,如果玩家 A 拿到葫芦,而玩家 B 拿到皇家同花顺,情况就不同了。

在我的项目中,我使用 UDP 作为主要通信方案,每个接收器发回通知。如果通信失败的时间超过 X,我就会诉诸 TCP。

My experience with UDP and TCP is like this:

  • UDP is significantly faster. we're talking 2 orders of magnitude.
  • On wired networks, UDP packet loss is less than 1%
  • On wireless, UDP packet loss can easily reach 80%, and the physical distance to the router matters: you'll lose 20% of packets from a foot away and 50% from 20 feet away.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文