使用 TCP 和 UDP 进行 Java 游戏 [英] Using TCP and UDP for java game

查看:48
本文介绍了使用 TCP 和 UDP 进行 Java 游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的朋友们制作一个可以在局域网上玩的实时 Java 游戏.该游戏在客户端-服务器架构中运行,并且现在一切都使用 UDP(用于位置更新和加入游戏).当我和我的朋友通过 Internet 测试游戏时,丢失了一些重要的 UDP 数据包(例如用于生成敌人的数据包).

I am making a real-time java game for my friends to play on LAN. The game is working in a client-server architecture, and it is using UDP for everything right now (for both position updates and joining to the game). When I tested the game with my friend over the Internet, a few important UDP packets were lost (like the one that is for spawning enemy).

所以我的问题是,制作实时多人游戏的最佳解决方案是什么?我可以将 UDP 用于必要的更新数据包,将 TCP 用于登录、断开连接、聊天等数据包吗?我可以在同一个端口和套接字上使用这两种协议吗?

So my question is, what is the best solution for making real-time multiplayer games, work? Can I use UDP for the necesarry update packets and TCP for packets like log-in, disconnect, chat etc. ? Can i use both protocol on the same port and socket?

推荐答案

你可以将两者结合起来.有时你可能想要可靠的数据,那个时候你需要 TCp.有时它不需要这样你可以使用 UDP.将两者结合起来.

you can combine both.Sometimes you may want reliable data ,that time you need TCp.Sometimes its not required so that time u can use UDP.Combine the both.

这篇关于使用 TCP 和 UDP 进行 Java 游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆