Java客户端/服务器基于帮助 [英] Java Client/Server turn based help

查看:145
本文介绍了Java客户端/服务器基于帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个基于客户端/服务器的游戏。我想这是一个2人的游戏。我将使用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.

Applet不能互相通信,所以通信必须通过服务器/线程(我假设)。

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

我不是Java的新手,但我从来没有做过任何网络。

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

推荐答案

在我看来,接近这种基于回合的游戏的最佳策略是决定一些基本的建筑方法。描述组件和一些基本的游戏流程图。

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. 与游戏引擎沟通

  2. 接受用户输入

  3. 解释游戏引擎响应

  4. 绘制屏幕

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

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

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.

所以它本质上是无状态的,但是某些技术(如Java servlet)将帮助您维护会话,以便您不必担心。不需要物理创建单独的线程,每个请求导致应用程序服务器产生一个新的执行线程,而会话本质是volatile。

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.

在服务器端I将持续会话中特定活动游戏的所有数据。

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. 播放器1发送结束回合请求,所有的

  2. 游戏引擎解释请求,对游戏状态进行必要的更改。

  3. 玩家2发送频繁请求以检查

  4. 游戏引擎确认玩家2的回合请求,并发送新的游戏状态回应。


  5. 冲洗并重复。

  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.

这篇关于Java客户端/服务器基于帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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