ArrayList for a multi-user chat program

inantamma

New Member
Hey there stack overflow.So I've been working on a chat program to teach myself some Java and I had it operational to the point where any amount of Clients could freely connect to the server, but each client could only communicate to the server. This is obviously a poor chat program so I implemented an arrayList of Handlers with the goal of sending what one Client writes to all Clients. Problem is I couldn't get it working and now my program crashes after I've typed 3 lines of text between a client and the server.Please take a look at my code. I will highlight the sections of code I changed when trying to implement the array list. I think I just stuck things into the wrong places.Server code: \[code\] import java.io.*; import java.net.*; import java.util.*;; public class Server{ //---------------------------------------------------- ArrayList<Handler> handlers = new ArrayList<Handler>(); //----------------------------------------------------public static void main(String[] args){ try{ ServerSocket ss = new ServerSocket(8822); while(true){ Socket s = ss.accept(); new Handler(s).start(); } }catch(Exception e){ System.out.println(e.getMessage()); }} } class Handler extends Thread{Socket socket;boolean notdone;BufferedReader br;PrintWriter pw;String line;public Handler(Socket socket){ this.socket = socket; notdone = true;}public void run(){ //------------------ handlers.add(this); //------------------ try{ br = new BufferedReader(new InputStreamReader(socket.getInputStream())); pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true); //-------------------------------------------------------- Iterator<Handler> handlerIterator = handlers.iterator(); //-------------------------------------------------------- while(notdone){ line = br.readLine(); if(line.equals("bye")){ System.out.println("Client said 'bye'"); notdone = false; break; }else{ System.out.println("Echo: " + line); //---------------------------------------------- while (handlerIterator.hasNext()){ Handler current = handlerIterator.next(); current.pw.println(line); } //---------------------------------------------- } } br.close(); pw.close(); socket.close(); }catch(Exception e){ System.out.println(e); System.out.println("Client severed connection."); }} }\[/code\]As for the client, I think I should change something in the highlighted section,but I'm not sure what.Client Code:\[code\]import java.io.*;import java.net.*;import java.util.*;public class Client{public static void main(String[] args){ try{ Socket s = new Socket("localhost", 8822); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true); Scanner scan = new Scanner(System.in); boolean notdone = true; while(notdone){ String outString = scan.nextLine(); if(outString.equals("bye")){ pw.println(outString); notdone = false; }else{ //------------------ pw.println(outString); String inString = br.readLine(); //------------------ System.out.println("Received: " + inString); } } br.close(); pw.close(); s.close(); }catch(Exception e){ System.out.println("Server severed connection."); }}}\[/code\]
 
Back
Top