3 Replies Latest reply: Mar 31, 2011 6:56 AM by 851783 RSS

    Java Server Threading Problems

    851783
      Cross-posted at: http://www.java-forums.org/networking/41574-server-threading-confusion.html with no answers as of this edit.

      I'm very new to networking in java, and it's been a few months since I last did any major programming. I decided to try out networking, and I'm beginning to get a feel for it, but I've jumped in to trying to make a server for a multiplayer game, and I've gotten a bit stuck about how to do it, specifically on how to use threads to take care of my clients, and still be able to keep track of the threads so I can have the server send out info to them about where characters are, when the threads should close because someone wants to log out, etc.
      If anyone could help me out that would be awesome :D
      Here's some code:

      import java.net.*;
      import java.io.*;
      
      public class GameThread extends Thread // this is the actual thread class I made
      {
      
        public Socket socket;
        public DataInputStream in;
        public DataOutputStream out; // a socket and two I/O streams for sending ints
      
        public GameThread(Socket socket) // typical constructor for getting socket
        {
          try
          {
            this.socket = socket;
            in = new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(socket.getOutputStream());
          }
          catch(Exception e)
          {
            e.printStackTrace();
          }
        }
      
        public Socket getSocket() // I don't believe this is ever used. Ignore it.
        {
          return socket;
        }
      
        public void print() // Used to make sure the thread is active/knows about the socket
        {
          System.out.println("GameThread says: " + socket);
        }
      
        public void run()
        {
          while(true)
          {
          }
        }
      }
      Here's the class I use to manage the threads, or, at least, where I attempt to do so.
      import java.io.*;
      import java.util.ArrayList;
      import java.net.*;
      
      public class Threader
      {
      
        public ArrayList clients;
        public ArrayList<GameThread> threads; // GameThread is the thread class above
      
        public Threader()
        {
          threads = new ArrayList<GameThread>(); //constructor for this arraylist
        }
      
        public void updateClientCount(ArrayList<Socket> clients) // this method works
        {
          this.clients = clients; // it's not the problem.
        }
      
        public void openThreads() //Here's probably where problems begin
        {
          if(clients.size() > threads.size()) // this is to make the threads arraylist
          {
            int f = clients.size()-threads.size();//equal length compared to clients arraylist
            while(f > 1)
            {
           threads.add(null);
           f--;
            }
          }
          if(clients.size() < threads.size()) //same as above, just if threads is bigger
          {
            threads.subList(clients.size(),(threads.size()-1)).clear();
          }
          for(int h=0;h<clients.size();h++) //Here: for each client spot
          {
            if(threads.get(h) == null) //Threads is tested in that spot to see if there's
            {              // A thread there
           GameThread gt = new GameThread((Socket)clients.get(h));
           threads.add(gt); //and if there isn't then it makes one
           gt.start(); // and starts it, but I guess this isn't happening as I want
            } // it to because in the method below none of the GameThreads print socket info
          }
        }
      
        public void print() //used to make sure the Threader/GameThread(s) is getting the info
        {
          for(int y = 0;y<clients.size();y++)
          {
            System.out.println("Threader says: " + clients.get(y));
          }
          for(int x = 0;x<threads.size();x++)
          {
            threads.get(x).print();
          }
        }
      }
      If anyone could help me find what is going on, or maybe (probably) I'm going about making a server wrong (I have another class with a serversocket and all, but I don't believe there are any bugs in it so it isn't included in here), so if I could get help that would be awesome.

      Thanks guys!

      Edited by: 848780 on Mar 30, 2011 8:55 PM

      Edited by: 848780 on Mar 30, 2011 11:55 PM
        • 1. Re: Java Server Threading Problems
          EJP
          Your thread management is back to front. All you need is a new thread per client, started every time you accept a new Socket. When you do that, if you want to manage them, enter the new thread into a collection, and when it exits remove it. But you normally don't need to keep track of client threads. You may want to keep track of client Sockets, in which case you should maintain a collection of Sockets on the same basis.
          • 2. Re: Java Server Threading Problems
            darrylburke
            Cross posted
            http://www.java-forums.org/networking/41574-server-threading-confusion.html

            Any more?

            db
            • 3. Re: Java Server Threading Problems
              851783
              I have just read about cross-posting. I'm sorry for doing it dude, I didn't think it angered people or annoyed them. I'll make sure to post links and let people know in the future. Thanks for the help.

              Edited by: 848780 on Mar 30, 2011 11:56 PM