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, e.g. 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 (e.g. 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.
Related
I am a new one in Java programming i want to know how overall concept of Socket and Server-Client work and how modification in one's system tends to the modification in other's system, As a starter i am making TicTacToe 2 player in java, i had made TicTacToe for Single Player but want some help to make it in multiplayer.I had gone through many question (on this site) before but they only solve some kind of problem but i want a full knowledge that how this all stuff works.
Can any one please tell me that how Server and Client do the message passing in Java ??
I want to ask where to write my Game Code so that it get loaded on both the devices and when user make a turn it get displayed into another user's device also...
There are many different answers/solutions for the use-case you described.
But never never develop network communication on a low-level layer. There are different solutions (api) to put the data(messages) into higher layers(f.e. HTTP). That enables you to turn your software very easy into a multithreaded and/or multisession-app.
I would prefer in your simple application an implementation with jersey and grizzly. You'll find lot of how-to's with google ("web services jersey grizzly") and here in stackoverflow of course. ;)
Firstly Cheers to all PROGRAMMERS [ Today = Programmers day :) ]
Secondly,
I'm working on a project where the specifications require using a server as a front end and an application in the back end. The project is an advanced smart home system. The server will handle commands coming from the client through the internet (let's say like a remote control from outside the house) and send them (through a channel of communication) to the application (planning on using JAVA application) which will handle the main logic like controlling hardware stuff (lights ...) , reading from a microphone (local mic) and accessing a database to act as a speech recognition system (offline).
Now I'm still in the planning phase and I'm not sure which technologies are the best for this project. I'm thinking to use Node.js or Apache as the server and a JAVA application as the back end and any SQL database for the application's SRS.
I hope this illustration demonstrates clearly how the system works:
The main question is:
What is the best way to make the Java application communicate with the server (communication channel [must be bidirectional]) ?
and Do you recommend a specific server other than the mentioned ones for this job ?
What crossed my mind so far:
1- JSP and servlets (making the server is the application too). But I don't want a server to handle the offline stuff and I'm not sure if java servlets can access hardware interface. I also want the server to be separate from making critical decisions (different layer for security reasons and since it won't be used as frequently as the local [offline] system).
2- Communication channel :
A- A shared file, but it's a bad idea since I don't want the application to check if the file contents changed (command received) or not from time to time (excessive operations).
B- A an inter-process-communication through a port (socket communication) seems the best solution but I don't know how that would turn in terms of operation cost and communication errors.
OS used : Linux Raspbian
EDIT:
I'm sure ZMQ+Apache is good enough for this task, but how is it in comparison to WebServices (like SOAP) ? Would WebServices be a better solution in terms of standard implementation and security ?
All related suggestions are welcomed, TQ
ZeroMQ is great for internal communications, or any other similar communication solutions.
For specifically your case, I can see that ZeroMQ would be a best fit.
Reasons:
You offline server have to be agnostic to web solution.
Communication can be reliable and bi-directional, possibly another patterns like (pub>sub, req<>res, etc).
Restarting any of sides would not require to restart the sockets (connection) on other side, as messages are queued.
Possibility to scale not just on same hardware, but as well to local area network or even through internet.
Big community of support. It might look a bit hard to get into, but in reality it is dead simple, just go to examples and once concept understood - it is very easy and neat to work with.
ZeroMQ has lots drivers for most popular languages, that includes Java and Node.js.
Considerations:
You need to think over packets and data will be sent. So some popular data protocols like XML or JSON is good way of thinking.
Responsibilities over different services - make sure they are not dependant on each other too much. Or if main offline server - is a core of system, make sure it does not depend on web facing service, so that web face can be removed/replaced/improved etc.
Few more points to think about:
Why Java, and what about modular approach? For example if you want to expand and scale - add more sensors into smart home solutions, then having one giant application would require to change it, it is harder to maintain as well as maintain different clients with own needs. Think modular way - some core functionality for offline stuff, but many aggregator processes that would talk to different sensors. This makes easier to support different setups and environments, as well maintain the system as a whole by improving independent components.
We've got a server app and two stand alone client apps (both with different functionality - one for front office and the other for back office). Everything is written in Java.
What we need right now:
If both apps are running - click on a button in one app -> checks to see if the other app is open and triggers some functionality (display a message, open a frame) on that app
if the other app isn't open -> it should display a message saying so.
Can anyone point me in the right direction to achieve this. The best real life example I can give is: how clicking on the an itunes link in the web browser opens the iTunes application if installed and to the relevant appstore page.
EDIT: Our applications don't deal with websites at all. Everything uses Swing.
There is no "best" way to achieve inter-app communications but there are many ways; the best one will be the one that fits best your environment: network conditions, firewalls, number of calls, synchronous vs asynchronous, etc...
Usually communication is achieved using either:
Remote Procedure Calls: an app basically calls a function/method on the other app and passes arguments. RPC are usually synchronous: the response is sent within the same communication/transaction
Messaging: an app sends messages to the other app which, maybe, replies with other messages. Messaging is usually asynchronous.
The frontier between the two can be pretty blur with some protocols like REST.
In the Java world,
RPC is usually achieved using either
RMI: Java only solution; easy to implement; does not like firwalls much.
SOAP Web services: not Java centric; hard to implement; full of traps; network friendly.
Messaging can be achieved using
JMS: Java only; rather easy to implement but asynchronous; extremely powerful on high loads
JSON/XML HTTP/s Messaging: there are many protocols here from the most secure like AS2 to RNIF, plain XML/Json POST etc... These are network and language agnostic but always require some work to implement.
An hybrid approach is REST which has become very popular due to the benefits of an easy implementation and network friendliness but has the drawbacks of not being very formalized. it is a technology rather than a specification. I would look at documentation around JAX-RS and frameworks like Restlet and Jersey to get you started.
(Edit)
I purposely did not mention developing your own with Java sockets. IO is by definition impure and often multithreaded: IO is very hard to get right. If you really insist going down that route, at least, use the help of a proper framework like Apache Mina or Netty.
I want to create a Java server in the client's browser to collect and manage communication
and update the displayed material in the client browser and receive the Get Push Post statements and then communicate through a long life persistent link for security reasons.
I know some universities have started working on this type of service
But i would prefer to do this from another approach.
What services/plugins are there that can help me in this field.
Its taken me a long time just to get to this point to even know what I'm asking for
But now I'm hearing I just need the tools and implementation of them.
What you're looking for is probably known as COMET, and can be done purely in JavaScript, without requiring a Java applet: http://en.wikipedia.org/wiki/Comet_(programming)) The advantage being that it could be implemented on a mobile device/tablet as well.
There are a number of libraries that will help you accomplish this depending on your programming environment. I tend to work in JSF, and so I favor tools for that environment, (IceFaces, RichFaces, PrimeFaces) but your choice of tools will depend on your specific application environment, which you haven't provided much detail about.
I am currently programming a Texas Hold'em LAN game in Java. My problem is how to do the client/server-communication.
Each time something happens at the table, the clients need to be informed of this so they can repaint their GUIs. Also when a players turn is up, they need to be able to fold, call etc.
How do I best implement this? I've looked at callbacks/RMI, but from what I've read, that may cause problems with firewalls(?)
When you are concerned about firewalls, the best way would be to use HTTP, like a web browser. The benefits are:
The protocol is well-known
There are many client and server libraries available, which are well-tested
HTTP is not restricted to web browsers. Any program can talk HTTP.
The downside is that push messages by the server are not commonly used. Of course you can just open an HTTP connection from each client that will wait for data from the server.
I would suggest the Java tutorials on sockets:
http://download.oracle.com/javase/tutorial/networking/sockets/
An example client/server is given.