server game in Java with applets

I have to implement a simple tour client-server game in Java. Unfortunately, I'm just beginning with network programming and have some problems with choosing an apropriate solution.

Let's assume I'm creating a chess game where two clients can connect to the server, authenticate with their username and passwords and play. The clients must be programmed as applets (thin clients), but I don't know what I should use as a server.

I mean, I've read about several different possibilities like RMI, sockets, servlets, but still don't know which one fits bets my needs. I'm a bit confused because I don't fully understand how the communication would be carried out.

Should I create an executable server which would run all the time on the server and wait for the players? This seems to me like an odd way. Or is there any easier way to do so, eg can I make a servlet and put it on Tomcat server so that the server would be run only if there are any players? Could that servlet communicate with applets (clients) and vice versa?*

I'd be really grateful for some tips.


can I make a servlet and put it on Tomcat server so that the server would be run only if there are any players

The Tomcat instance would run anyways, otherwise players couldn't connect to it.

What you could do is to provide a server that starts a new game instance when players connect. The server itself would have to always run.

In terms of technology, I'd suggest you use whatever you feel comfortable with. Don't care about performance yet but try and get started.

So if you already have some knowledge with a communication technology, try and use that. Just be aware of the limitations and take those into account (eg message formats, push/pull communication etc.).


It depends what kind of game you are after. Applets are usually good choice for presenting animation (completely in Java) and accessible from a browser. Real world examples would divert towards Flash for client presentation.

If your game is a turn type game (chess, cards etc.), then you can implement your logic in form of servlets or web services or ajax, with appropriate use of hashtables or databases to store live sessions on server side. If your game is more involved in terms of user experience (take an example of Need For Speed type, for instance), then creating a custom server make more sense.


If you are looking for a netwrok application framework in Java then you may consider reading about Apache MINA. Documentation claims that it " helps users develop high performance and high scalability network applications easily." and it has support for various transports such as TCP/IP and UDP/IP via Java NIO. Summary of features can be seen here.

My personal experience with MINA is so far good and used in various projects. One implementation resembles your case, Its not a mulitplayer game but do involves multiple applets connecting a server. I found MINA very good in handling multiple sessions. It do it very neatly. Moreover its very easy to scale and maintain code. Easy to add filters and define protocols.

There are no. of good tutorial available to jump start and initial setup is very easy to do.

However, like any emerging opensource project, it has its problems too. That are: Online community is small and documentation, though improving rapidly, is very limited.

Official user guide covers most of the basics and is a good starting point if you want to know more.

链接地址: http://www.djcxy.com/p/9562.html

上一篇: 如何从Blu中读取标题和ID

下一篇: 带有小程序的Java服务器游戏