Splasher
Splasher

Reputation: 47

Server not sending messages to the clients connected to it

I have created a chat server which is multi threaded and as and when the clients connect it initiates a separate thread for them to communicate. The problem is the my server is not broadcasting messages received from one client to all the other clients connected and I simply do not know how to implement it. Following is my code, feel free to edit:

The serverclass:

ArrayList<ServerThread> clientlist  = new ArrayList();
Hashtable clientList = new Hashtable();
private static ArrayList<PrintWriter> writers = new ArrayList<PrintWriter>();


public ChatServer(JTextField jtfA, JTextField jtfB, JTextArea jtaA, JTextArea tapane3) {
    // TODO Auto-generated constructor stub
address= jtfA;
GETPORT=jtfB;
PORT=Integer.parseInt(jtfB.getText());
displaytext=jtaA;
clientside=tapane3;

}


public void run() {
    // TODO Auto-generated method stub

    try {
        InetAddress ad=InetAddress.getLocalHost();

        String ip=ad.toString();


                    address.setText(string);
                    address.setEditable(false);


        System.out.println("Server IP is: " + ad);
        ss = new ServerSocket (PORT);

        Socket cs = null;
        while (true)
       {

            cs=ss.accept();
           System.out.println("Connected"+clientlist);

         ServerThread handler = new ServerThread(cs,displaytext,clientside,writers);
         clientlist.add(handler);
         handler.start();
        System.out.println(writers);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}


@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub


    if(e.getActionCommand().equals("DisConnect")) 
    {
          try {
            ss.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }           
    }
  }
 }

The server thread class::

public class ServerThread extends Thread {

  Socket client;
  JTextArea display;
  JTextArea clients;
  ServerSocket server;
  ArrayList<PrintWriter> writers ;
public ServerThread(Socket cs, JTextArea displaytext, JTextArea clientside, ArrayList<PrintWriter> writers) {
    // TODO Auto-generated constructor stub

client=cs;
display=displaytext;
clients=clientside;
this. writers=writers;



}
public void run () {


    try{
    BufferedReader br = new BufferedReader(
            new InputStreamReader(
                    client.getInputStream()));
    PrintWriter pr = new PrintWriter(
            new OutputStreamWriter(
                    client.getOutputStream()));


    String clientMsg="";
    boolean all;
        do {


    //read msg from client

    clientMsg = br.readLine();
    //display.setText(clientMsg);
    System.out.println("Server Received: " + clientMsg);
    pr.println(clientMsg);
    display.append(clientMsg+"\n");
    clients.append(clientMsg+"\n");
    pr.flush();

    } while((all=br.readLine() != null));


    br.close();
    pr.close();


    while (true) {
         String input = br.readLine();
         if (input == null) {
             return;
         }
         for (PrintWriter writer : writers) {
             writer.println("MESSAGE " + ": " + input);
         }

    }}
catch (UnknownHostException uhe){

    System.out.println("Server issue");


}
catch (IOException ioe){

    System.out.println("Server Error IO");

}
catch (Exception e){

    System.out.println("Server Error General");

}}
}

Upvotes: 0

Views: 44

Answers (1)

user207421
user207421

Reputation: 310840

Faulty do/while loop. You're throwing away every second line, and not detecting end of stream correctly. It should be:

while ((line = br.readLine()) != null)

Upvotes: 1

Related Questions