Results 1 to 3 of 3
Thread: Shared array between Threads?
- 04-15-2012, 08:02 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 20
- Rep Power
- 0
Shared array between Threads?
Hello, and hi Java-forums.org!
I just started up learning java and im currently creating a Multithreaded server.
So each client connecting to the server is put into a Thread and then it does it magic. Everything is working fine except the part where the server needs to echo back client message to ALL clients. currently its only working 1 on 1
(Server <--> Client).
So, my plan is to create a shared array that which stores all the client objects, and then it would loop through the array sending to every client.
I have no idea if that even works or if there is any better way of doing it (have been googling for the last two hours).
So here is my client_thread code
Yeh thats pretty much it. ive been trying to use this the array calledJava Code:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; public class client_threads extends Thread { Socket clientSocket; int clientID = -1; boolean bRunThread = true; public Object[] clients = new Object[10]; client_threads(Socket socket, int id) { clientSocket = socket; clientID = id; clients[id] = clientSocket; } public void run() { BufferedReader in = null; PrintWriter out = null; //Print ut detaljene av den aksepterte klienten. System.out.println("Accepted client : ID - " + clientID + " : Address - " + clientSocket.getInetAddress().getHostName()); try{ in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream())); while(bRunThread) { //Read incoming stream String clientRead = in.readLine(); System.out.println("Client" + clientID + " says: " + clientRead); if(clientRead.equalsIgnoreCase("quit")) { bRunThread = false; System.out.println("Stopping the thread for "+ clientID); } else { //Echo back to client out.println(clientRead); out.flush(); } } }catch(Exception e){e.printStackTrace();} finally { try{ //Cleaningoutthecloset in.close(); out.close(); clientSocket.close(); }catch(IOException e){e.printStackTrace();} } } }but didnt have any luck in sharing it.Java Code:Object[] clients = new Object[10];
All help is appreciated!
Thank
Driiper
- 04-15-2012, 04:53 PM #2
Re: Shared array between Threads?
Using an array of Object is not the way to do it. Use an ArrayList that is defined to hold client class objects.
If you don't understand my response, don't ignore it, ask a question.
- 04-16-2012, 09:58 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Threads per Connection or Threads per Request
By Felic in forum New To JavaReplies: 4Last Post: 11-22-2011, 09:15 PM -
Question About Shared Objects
By mattloto in forum New To JavaReplies: 1Last Post: 02-08-2011, 02:43 AM -
Adding elements in array list and vectors using threads
By hina.yousuf@seecs.edu.pk in forum Advanced JavaReplies: 2Last Post: 10-10-2010, 03:07 AM -
Scannig for shared files
By Falcon_minsk in forum NetworkingReplies: 0Last Post: 11-07-2009, 11:29 AM -
How an array of Threads works...
By Moncleared in forum New To JavaReplies: 6Last Post: 09-09-2009, 01:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks