Results 1 to 3 of 3
  1. #1
    driiper is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default 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
    Java 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();}
    		}
    	}
    }
    Yeh thats pretty much it. ive been trying to use this the array called
    Java Code:
    Object[] clients = new Object[10];
    but didnt have any luck in sharing it.


    All help is appreciated!

    Thank
    Driiper

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

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

  3. #3
    driiper is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Shared array between Threads?

    Quote Originally Posted by Norm View Post
    Using an array of Object is not the way to do it. Use an ArrayList that is defined to hold client class objects.
    sweeeet. it works!

    Thank you :)

    driiper

Similar Threads

  1. Threads per Connection or Threads per Request
    By Felic in forum New To Java
    Replies: 4
    Last Post: 11-22-2011, 09:15 PM
  2. Question About Shared Objects
    By mattloto in forum New To Java
    Replies: 1
    Last Post: 02-08-2011, 02:43 AM
  3. Adding elements in array list and vectors using threads
    By hina.yousuf@seecs.edu.pk in forum Advanced Java
    Replies: 2
    Last Post: 10-10-2010, 03:07 AM
  4. Scannig for shared files
    By Falcon_minsk in forum Networking
    Replies: 0
    Last Post: 11-07-2009, 11:29 AM
  5. How an array of Threads works...
    By Moncleared in forum New To Java
    Replies: 6
    Last Post: 09-09-2009, 01:40 AM

Posting Permissions

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