1

I've been trying to work out how to approach this problem but I don't know where to start.

I have an game applet written in Java that I'm trying to make multi-player across a network. At the moment it is a standalone application but eventually it needs to be on some sort of web page where clients can access it through a browser and play against each other.

I was thinking that each clients applet could communicate using sockets with an applet that runs constantly on a tomcat server. This applet on the server would handle incoming socket connections, create new threads to handle each connection, maintain a list of connected clients, and set up games. Clients could also get information from a database about past games etc. by communicating with the server.

For example I want a client to be able to start a game. Then other clients are notified that he has started a game and can join the game. Then when the host clicks start game, all the other clients are notified and the game begins in their applet. Whoever solves the puzzle first would then click finished. His score would be sent to the server applet which then forwards the result to each client connected.

So i have some questions

  1. Does this sound like a reasonable plan?

  2. How does tomcat assist with this?

  3. How would i deploy it to tomcat? I'm doing this in eclipse and have set added a local tomcat server. Do i just run the class on the server with eclipse, then socket connect to http://localhost/packagename/classname on the applet?

  4. What classes would the servlet need to extend to make it connectable? Since connections will be made from an applet should the servlet be a Non-Http Servlet?

  5. Can you recommend some documentation or provide some example code of a client applet communicating with a server applet using tomcat? All the examples I've seen are just html forms that pass information to a servlet using Http.

Sbram
  • 65
  • 1
  • 2
  • 6
  • "Java applet communicate with applet on tomcat server". Surely, you mean a "servlet" running on the Tomcat server, don't you? – Bruno Jun 17 '11 at 18:12
  • It's actually throughout the text too. – Bruno Jun 17 '11 at 18:19
  • This may be helpful: http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests – BalusC Jun 17 '11 at 19:33

2 Answers2

1

If your application doesn't need real-time communication with a server over a raw socket (and it sounds like yours does not), you might be better off using standard web protocols for your applet to communicate with the server.

One choice might be HTTP/JSON. Your applet could make an HTTP connection to a servlet, and that servlet will generate a JSON object that represents a message the server wants to send to the client. The client will start a thread that will loop, making an asynchronous blocking call to your servlet to poll for new data.

The major advantage of this approach, is that in the future, should you be so inclined, you can ditch the client-side java applet, and replace it with HTML5 and Javascript.

GSON is a library to use for JSON serialization/deserialization. And java.net.URL is what you would use to connect to the servlet inside your applet.

ironchefpython
  • 3,343
  • 1
  • 17
  • 32
  • Do you have any documentation that shows an applet communicating with a servlet with http? I would be fine with just passing simple text messages between the two and using a switch to process what action to take. Also any you can say about my tomcat questions? – Sbram Jun 17 '11 at 19:32
  • @Sbram http://www.devdaily.com/java/edu/pj/pj010023 has useful examples. And Tomcat doesn't add anything special, you'll just be using it as a standard servlet container. – ironchefpython Jun 17 '11 at 19:35
0

Yes. What you are describing is applet-servlet communication. Typically, your applet will send messages to the servlet which will then keep track of communicating with all the other client applets.

There are several examples of applet-servlet communications online. Here is one -- old, but still valid and the code is not formatted.

http://docstore.mik.ua/orelly/java-ent/servlet/ch10_01.htm

There are security restrictions around applets and the servers they can communicate to, so thats something else to keep in mind.

Kal
  • 23,894
  • 7
  • 58
  • 61
  • So since the applet will be communicating directly with the servlet do I need a Non Http servlet? Does the applet have to be played via the tomcat server ie accessed by localhost:8080 or can it be anywhere on the network? Basically could you provide some more clarification for question 3 please. – Sbram Jun 17 '11 at 18:39