服务器不向与其连接的客户端发送消息

时间:2015-05-17 13:44:23

标签: java multithreading client server

我创建了一个多线程的聊天服务器,当客户端连接它时,会启动一个单独的线程供他们进行通信。问题是我的服务器没有将从一个客户端收到的消息广播到所有其他连接的客户端,我根本就不知道如何实现它。 以下是我的代码,随时可以编辑:

服务器类:

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();
        }           
    }
  }
 }

服务器线程类::

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");

}}
}

1 个答案:

答案 0 :(得分:1)

do / while循环错误。你丢掉每一行,而不是正确地检测流的结尾。它应该是:

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