Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. digital sinner
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    sinner's land
    Posts
    68
    Rep Power
    12

    communication with other threads


    i have a server and for each connection with each client a create a thread of a ClientWorker class. i receive text from one client and i want to transmit it through the server prg. at each client. how do i do that?
  2. #2
  3. digital sinner
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    sinner's land
    Posts
    68
    Rep Power
    12
    come on! does anyone know?
  4. #3
  5. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Your question is too broad. Post what code you have and some more specific questions and you may get more help. Exactly what are you having problems with?
  6. #4
  7. digital sinner
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    sinner's land
    Posts
    68
    Rep Power
    12
    well....i create a thread implementing the Runnable interface on a class. within that class i want to send text to all the other clients when it's received.
  8. #5
  9. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Do you want us to write it for you? Post any code you have and ask specific things about the code in question if you want some feedback. If it is blowing up, we need stack traces and exact behavior. You will find that people will help you as much as you appear to be willing to help yourself. As far as anyone can tell, you ahve put no effort towards this problem.
  10. #6
  11. digital sinner
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    sinner's land
    Posts
    68
    Rep Power
    12
    import java.net.*;
    import java.io.*;

    class Client_ implements Runnable {
    private Socket clientSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;

    Client_(Socket client) {
    this.clientSocket = client;
    }
    public synchronized void run(){
    String line = null;
    try {
    out = new PrintWriter(clientSocket.getOutputStream(),true);
    in = new BufferedReader((new InputStreamReader(clientSocket.getInputStream())));
    }catch (IOException err) {System.out.println("Error: " + err);};
    while (true) {
    try {
    line = in.readLine();
    }catch (IOException err) {/*System.out.println("Error: " + err);*/break;};
    System.out.println(line);
    //this is where i want to send the text i've received from this client to all the other clients
    }
    }
    }

    public class server {
    static ServerSocket server = null;
    static int port;

    public static void main(String [] args) {
    port = 6666;
    System.out.println("Server is started...");
    listenSocket(port);
    }
    public static void listenSocket(int port) {
    try {
    server = new ServerSocket(port);
    }
    catch (IOException err) {System.out.println("Error: " + err);};
    threads = new ThreadGroup("clients");
    while (true) {
    Client_ worker;
    try {
    worker = new Client_(server.accept());
    new Thread(threads,worker).start();
    }catch(IOException err) {System.out.println("Error: " + err);};
    }
    }
    protected void finalize() {
    try {
    server.close();
    }catch(IOException err) {System.out.println("Error: " + err);}
    }
    }
  12. #7
  13. digital sinner
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    sinner's land
    Posts
    68
    Rep Power
    12
    sorry...but i thought it's too much code for a forum.
  14. #8
  15. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Ok, the problem as I see it on a higher level is that you have no references to what clients are connected at any given time. The server needs a way to store connected clients. Then your Client_ class will need to take a reference to the server object that created it. After the one client is done doing what it needs to do, it noitifies the server to tell the same thing to all the other clients connected to it. Make sense?

    In other words, the server will need to keep a collection of Client_ objects (in a Vector or something) and when one client says, the server will need to iterate through that collection and pass the message along.

    Also, if you wrap your code in [ code] code here [ /code] tags it will make it easier to read:
    Code:
    class Client_ implements Runnable {
    	private Socket clientSocket = null;
    	PrintWriter out = null;
    	BufferedReader in = null;
    
    	Client_(Socket client) {
    		this.clientSocket = client;
    	}
    	public synchronized void run() {
    		String line = null;
    		try {
    			out = new PrintWriter(clientSocket.getOutputStream(), true);
    			in =
    				new BufferedReader(
    					(new InputStreamReader(clientSocket.getInputStream())));
    		}
    		catch (IOException err) {
    			System.out.println("Error: " + err);
    		};
    		while (true) {
    			try {
    				line = in.readLine();
    			}
    			catch (IOException err) { /*System.out.println("Error: " + err);*/
    				break;
    			};
    			System.out.println(line);
    			//this is where i want to send the text i've received from this client to all the other clients 
    		}
    	}
    }
    
    public class server {
    	static ServerSocket server = null;
    	static int port;
    
    	public static void main(String[] args) {
    		port = 6666;
    		System.out.println("Server is started...");
    		listenSocket(port);
    	}
    	public static void listenSocket(int port) {
    		try {
    			server = new ServerSocket(port);
    		}
    		catch (IOException err) {
    			System.out.println("Error: " + err);
    		};
    		threads = new ThreadGroup("clients");
    		while (true) {
    			Client_ worker;
    			try {
    				worker = new Client_(server.accept());
    				new Thread(threads, worker).start();
    			}
    			catch (IOException err) {
    				System.out.println("Error: " + err);
    			};
    		}
    	}
    	protected void finalize() {
    		try {
    			server.close();
    		}
    		catch (IOException err) {
    			System.out.println("Error: " + err);
    		}
    	}
    }
  16. #9
  17. digital sinner
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    sinner's land
    Posts
    68
    Rep Power
    12
    ok Nemi...i got the ideea...thanx and sorry if i bother you :P...but imaybe i'll do it again :P
  18. #10
  19. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Np. Just post your code and ask your questions. Remember, you generally can't give too much info when asking a question.
  20. #11
  21. digital sinner
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    sinner's land
    Posts
    68
    Rep Power
    12
    Code:
    import java.net.*;
    import java.io.*;
    //import java.util.*;
    
    class Client_ implements Runnable {
    	private Socket clientSocket = null;
    	private int no;
    	PrintWriter out = null;
    	BufferedReader in = null;
    	
    	Client_(Socket client, int no) {
    		this.clientSocket = client;
    		this.no = no;
    		}
    	public synchronized void run(){
    		String line = null;
    		try {
    			out = new PrintWriter(clientSocket.getOutputStream(),true);
    			in = new BufferedReader((new InputStreamReader(clientSocket.getInputStream())));
    		}catch (IOException err) {System.out.println("Error: " + err);};
    		while (true) {
    			try {
    				line = in.readLine();
    			}catch (IOException err) {/*System.out.println("Error: " + err);*/break;};
    			System.out.println(line);
    /*			for (int i=0;i<=server.output.length;i++) 
    				if (i!=this.no) server.output[i].println(line);*/
    				};
    			}
    	}
    
    public class server {
    	static ServerSocket server = null;
    	static int port;
    	static PrintWriter [] output = null;
    			
    	public static void main(String [] args) {
    		port = 6666;
    		System.out.println("Server is started...");
    		listenSocket(port);
    		}
    	public static void listenSocket(int port) {
    		try {
    			server = new ServerSocket(port);
    			}
    			catch (IOException err) {System.out.println("Error: " + err);};
    		while (true) {
    			Client_ worker;
    			try {
    				//the next line produces an exception and i don't know why
    				worker = new Client_(server.accept(),output.length+1);
    				new Thread(worker).start();
    				output[output.length+1] = worker.out;
    			}catch (IOException err) {System.out.println("Error: " + err);}
    			catch (NullPointerException err) {System.out.println(err);};
    			}
    		}
    	protected void finalize() {
    		try {
    			server.close();
    		}catch(IOException err) {System.out.println("Error: " + err);}
    		}
    	}
    i am trying to create an array with all the PrintWriters i use for each client that connect to the server and then send the text i receive from one client to all the other clients.
  22. #12
  23. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    What kind of exception are you getting? If it's a NullPointerException, it's probably because you haven't initialized output, so it's still null.
  24. #13
  25. digital sinner
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    sinner's land
    Posts
    68
    Rep Power
    12

    Talking


    sorry...i posted the wrong code....
    this is the code and i'm getting the exception at the bolded line
    the exception is NullPointerException and i get it when i connect 2 or more clients to the server prg
    Code:
    import java.net.*;
    import java.io.*;
    
    class Client_ implements Runnable {
    	private Socket clientSocket = null;
    	int no;
    	PrintWriter out = null;
    	BufferedReader in = null;
    	
    	Client_(Socket client, int no) {
    		this.clientSocket = client;
    		this.no = no;
    		}
    	public synchronized void run(){
    		String line = null;
    		try {
    			out = new PrintWriter(clientSocket.getOutputStream(),true);
    			in = new BufferedReader((new InputStreamReader(clientSocket.getInputStream())));
    		}catch (IOException err) {System.out.println("Error: " + err);};
    		while (true) {
    			try {
    				line = in.readLine();
    			}catch (IOException err) {System.out.println("The client is disconnected!");break;};
    			System.out.println(no + ": " + line);
    			for (int i=1;i<=server.contor;i=i+1) 
    				if (i != this.no) server.output[i].println(line);
    				};
    			}
    	}
    
    public class server {
    	static ServerSocket server = null;
    	static int port;
    	static PrintWriter [] output = null;
    	static int contor =0;
    			
    	public static void main(String [] args) {
    		port = 6666;
    		System.out.println("Server is started...");
    		listenSocket(port);
    		}
    	public static void listenSocket(int port) {
    		try {
    			server = new ServerSocket(port);
    			}
    			catch (IOException err) {System.out.println("Error: " + err);};
    		output = new PrintWriter [256];
    		while (true) {
    			Client_ worker;
    			try {
    				worker = new Client_(server.accept(),contor+1);
    				new Thread(worker).start();
    				output[contor+1] = worker.out;
    				contor++;
    			}catch (IOException err) {System.out.println("Error: " + err);}
    			catch (NullPointerException err) {System.out.println(err);};
    			}
    		}
    	protected void finalize() {
    		try {
    			server.close();
    		}catch(IOException err) {System.out.println("Error: " + err);}
    		}
    	}
    Last edited by heinrich; July 2nd, 2003 at 04:17 PM.
  26. #14
  27. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    When you call start() on your thread, it runs the run() method of the Runnable object passed to it - your client in this case. It is possible that the client thread is trying to call this line

    server.output[i].println(line);

    before it reaches this line in the server

    output[contor+1] = worker.out;

    So that the servers output array still has null variables in it. By rearranging the order in whcih you call your methods you can likely avoid this.

    Code:
    worker = new Client_(server.accept(),contor+1);
    output[contor+1] = worker.out;
    contor++;
    new Thread(worker).start();
    Personally, I wouldn't leave it up to the clients to send messages to other clients. I would make a method in your server that takes a String and sends that to all clients.
  28. #15
  29. digital sinner
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    sinner's land
    Posts
    68
    Rep Power
    12
    i still get the exception.
    could you give me an example of the method you mentioned, plz?
    Last edited by heinrich; July 2nd, 2003 at 04:52 PM.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo