Results 1 to 3 of 3
  1. #1
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default Server Threading Confusion

    Cross-posted at http://forums.oracle.com/forums/thre...83043&#9483043 with a helpful answer there.

    I'm trying to learn how to make a server for a multiplayer game, and at the moment I'm having troubles getting the threads that will send clients data to do anything. I'll explain throughout the code

    Here's the actual class I use for each thread:

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

    Java Code:
    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!
    Last edited by Fortu; 03-31-2011 at 07:40 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

  3. #3
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    None other. Sorry for not specifying that this was cross posting. Similar to my post on sun's forums, I had no idea it had consequence at all.
    Last edited by Fortu; 03-31-2011 at 07:39 AM.

Similar Threads

  1. threading in server connection
    By niteangell21 in forum New To Java
    Replies: 1
    Last Post: 10-15-2010, 11:06 PM
  2. threading help
    By the reporter in forum AWT / Swing
    Replies: 19
    Last Post: 06-21-2010, 05:50 PM
  3. Threading
    By hedonist in forum Advanced Java
    Replies: 3
    Last Post: 03-13-2010, 02:21 PM
  4. Threading
    By jon80 in forum New To Java
    Replies: 1
    Last Post: 06-13-2009, 10:53 PM
  5. Threading in EJB
    By java08 in forum Advanced Java
    Replies: 2
    Last Post: 08-12-2008, 11:09 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •