Java 客户端/服务器回合制帮助

发布于 2024-10-31 21:15:32 字数 254 浏览 0 评论 0原文

我正在尝试制作客户端/服务器回合制游戏。我希望这是一款 2 人游戏。我将使用 Java 小程序作为客户端,以便人们可以通过浏览器在线玩游戏。

到目前为止我所知道的是,我可以创建一个接受所有传入连接的服务器,并创建一个线程来处理连接的客户端。我还可以编写连接到服务器的客户端。我不知道如何让两个单独的客户端相互交互。

小程序无法相互通信,因此必须通过服务器/线程完成通信(我假设)。

我对Java并不陌生,但之前从未接触过任何网络。有人可以帮我吗?

I am trying to make a client/server turn based game. I want this to be a 2 player game. I will be using a Java applet as the client so that people can play online through a browser.

What I know so far is that I can create a server that accepts all incoming connections and create a thread to handle clients that connect. I can also write the client that will connect to the server. What I don't know is how to get two separate clients to interact with each other.

Applets cannot talk to each other so communication must be done via the server/threads (I assume).

I am not new to Java, but I have never done any networking before. Can someone help me out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

も让我眼熟你 2024-11-07 21:15:33

在我看来,处理此类回合制游戏的最佳策略是决定一些基本的架构方法。绘制出组件和一些基本的游戏流程图。

您应该将大部分游戏引擎逻辑放在服务器组件中。客户端应尽可能保持精简,主要关注

  1. 与游戏引擎的通信
  2. 接受用户输入
  3. 解释游戏引擎响应
  4. 绘制屏幕

您的服务器/游戏引擎应该是相对无状态的,但维护当前正在运行的游戏会话的列表。有状态 SOAP Web 服务甚至 HTTP Servlet 都是不错的选择,因为它们通过在请求中放置和读取会话 cookie 来为您维护会话。

所有 Web 内容都基于请求响应,因此本质上是无状态的,但某些技术(例如 Java servlet)将帮助您维护会话,这样您就不必这样做。不需要物理上创建单独的线程,每个请求都会导致应用程序服务器生成一个新的执行线程,而会话本质上是易失性的。

在服务器端,我将保留会话中特定活动游戏的所有数据。这样,你的游戏引擎就会维持两个玩家之间的有序沟通。

  1. 玩家 1 发送回合结束请求以及所有游戏状态更改信息。
  2. 游戏引擎解释请求,对游戏状态进行必要的更改。
  3. 玩家 2 频繁发送请求来检查是否轮到玩家 2。
  4. 游戏引擎确认玩家 2 的回合请求并发送新的游戏状态作为响应。
  5. 玩家 2 接收响应,更新其游戏状态副本,记录自上一回合以来的变化。
  6. 冲洗并重复。

In my opinion the best strategy to approach a turn based game such as this, is to decide on some basic architectural approaches. Diagram out the componenents and some basic game flow diagrams.

You should put the bulk of the game engine logic in the server component. The clients should be kept as thin as possible, focusing primarily on

  1. Communication with the game engine
  2. Accepting user inputs
  3. Interpreting game engine responses
  4. Drawing the screens

Your server/game engine should be relatively stateless, yet maintain a list of current game sessions in play. Stateful SOAP web services or even HTTP Servlets would be a good choice because they maintain session for you by placing and reading session cookies in the request.

Everything web works on request response so it is by nature stateless, but certain technologies like Java servlets will help you maintain sessions so that you don't have to. No need for physically creating seperate threads, each request causes the application server to spawn a new thread of execution, while the session by nature is volatile.

On the server side I would persist all data for a particular active game in the session. In this way, your game engine will maintain the orderly communication between the two players.

  1. Player 1 sends end of turn request with all of the game state change information.
  2. Game engine interprets request, makes necessary changes to the game state.
  3. Player 2 sends frequent requests to check and see if it is Player 2s turn yet.
  4. Game engine acknowledges Player 2 request for its turn and sends the new game state in response.
  5. Player 2 receives the response, updates its copy of the game state making note of the changes since its last turn.
  6. Rinse and repeat.
别想她 2024-11-07 21:15:33

您只需使用服务器作为中间人。

  1. 客户端 A 向服务器发送(写入)消息,属性将客户端 B 指定为目标
  2. 服务器接收(读取)消息并将消息转发(写入)给客户端 B
  3. 客户端 B 接收(读取)消息。

You just use the server as a middle-man.

  1. Client A sends (writes) message to Server with attribute denoting Client B as destination
  2. Server receives (reads) the message and forwards (writes) the message to Client B
  3. Client B receives (reads) the message.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文