Results 1 to 3 of 3
Thread: Server Threading Confusion
- 03-31-2011, 03:51 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 57
- Rep Power
- 0
Server Threading Confusion
Cross-posted at http://forums.oracle.com/forums/thre...83043� 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:
Here's the class I use to manage the threads, or, at least, where I attempt to do so.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) { } } }
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.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(); } } }
Thanks guys!Last edited by Fortu; 03-31-2011 at 07:40 AM.
- 03-31-2011, 06:30 AM #2
Cross posted
OTN Discussion Forums : Java Server Threading Problems ...
Any more?
db
- 03-31-2011, 07:27 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 57
- Rep Power
- 0
Similar Threads
-
threading in server connection
By niteangell21 in forum New To JavaReplies: 1Last Post: 10-15-2010, 11:06 PM -
threading help
By the reporter in forum AWT / SwingReplies: 19Last Post: 06-21-2010, 05:50 PM -
Threading
By hedonist in forum Advanced JavaReplies: 3Last Post: 03-13-2010, 02:21 PM -
Threading
By jon80 in forum New To JavaReplies: 1Last Post: 06-13-2009, 10:53 PM -
Threading in EJB
By java08 in forum Advanced JavaReplies: 2Last Post: 08-12-2008, 11:09 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks