I teach an intro to programming class at a high school and I am trying to give a networking project for my students. I was thinking that having them do a battleship type game would be very doable and I think they would really get jazzed up about it. I can do this fine using sockets on my home computer, but at school all ports are closed and they aren't opening them up for me.
Is there a work around (like have a web service deal with relaying info around)? Any ideas?
(The class is basic and students don't have much background - I need to make it as simplified as possible).
This servlet should work as advertised in the comment (haven't tested it):
Send a message with the URL parameters to
and message
Receive a message with get
(receiver id). Returns a message, or nothing if there is none. Should be testable with a browser.
In the real world, a get request should not really change the state of the server. I would only run this for the time of the class, as anybody can send stuff to any id, and anybody can read stuff for any id.
public class SimpleMessageServlet extends HttpServlet {
private Map<String,String> messages = new HashMap<String,String>();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String to = request.getParameter("to");
String message = request.getParameter("message");
if (to != null && from != null && message != null) {
messages.put(to, message);
}
response.setContentType("text/plain");
String get = request.getParameter("get");
if (get != null) {
String result = messages.remove(get);
if (result != null) {
PrintWriter out = response.getWriter();
out.println(result);
}
}
}
Edit: Simplified to store only one message per address, should be enough for a turn based game and buffering will just add complexity to the client logic (clearing out outdated turn data from previous matches etc.). Probably makes sense to sync up clients somehow, e.g. send a "start" signal before sending turns, and to discard anything until "start" is received. Sad thing is that this will require polling, using peer to peer sockets would be much nicer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With