Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Networking with closed ports at a school

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).

like image 706
user2260700 Avatar asked Dec 03 '13 20:12

user2260700


1 Answers

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.

like image 58
Stefan Haustein Avatar answered Nov 02 '22 07:11

Stefan Haustein