Tuesday, July 3, 2012

Just looking at some of the projects I've worked on in the past. One of my favorite was a group project me and some guys from some of my classes did was this networked bingo game. You can download the .jar files and view all the source code here.

We used threads to run the server based part (originally it was a stand alone game and then we networked it, so we tried to keep most of the original structure intact). Here's the run function:


public void run()
   {
      try
      {
         //Set the port number
         int port = 1352;

         //Establish the listen socket.
         ServerSocket welcomeSocket = new ServerSocket(port);


         InetAddress addr = InetAddress.getLocalHost();
         String ip = addr.getHostAddress();

         mMessage.setText("   Server IP Address: " + ip);

         //Process service requests in an infinite loop
         while (true)
         {
            //Listen for a TCP connection request
            Socket connectionSocket = welcomeSocket.accept();
            // Construct an object to process the request.
            BingoHandler request = new BingoHandler(connectionSocket);
            // Create a new thread to process the request.
            Thread thread = new Thread(request);
            // Start the thread.
            thread.start();
         }
      }
      catch (Exception e)
      {
      }
   }


Some simple stuff, but we were really proud to get it working.

//END OF LINE

No comments:

Post a Comment