如何从客户端/服务器聊天程序中的所有客户端中的列表中删除名称

时间:2016-05-06 16:15:58

标签: java

我正在进行客户端/服务器聊天,并且在一个客户端断开连接时,在从所有打开的客户端上的列表中删除名称时遇到问题。对,我有它似乎它在客户端之间广播列表,但它没有删除任何名称。任何帮助都是极好的。这个项目的价值是我最终成绩的50%。

聊天服务器代码:

 class ChatHandler extends Thread
    {  public ChatHandler(Socket i, ArrayList<ChatHandler> h) 
     { 
        incoming = i;
        handlers = h;
        handlers.add(this);

        try{
            in = new ObjectInputStream(incoming.getInputStream());
            out = new ObjectOutputStream(incoming.getOutputStream());
        }catch(IOException ioe){
                System.out.println("Could not create streams.");
        }
     }
     public synchronized void broadcast(){

        ChatHandler left = null;
        for(ChatHandler handler : handlers){
            ChatMessage cm = new ChatMessage();
            cm.setMessage(myObject.getMessage());
            cm.setName(myObject.getName());


            try{
                handler.out.writeObject(cm);

                System.out.println("Writing to handler outputstream: " + cm.getMessage());
            }catch(IOException ioe){
                //one of the other handlers hung up
                left = handler; // remove that handler from the arraylist   
            }
        }
        handlers.remove(left);

        if(myObject.getMessage().equals("bye")){ // my client wants to leave
            done = true;    
            handlers.remove(this);
            System.out.println("Removed handler. Number of handlers: " + handlers.size());
        }
        System.out.println("Number of handlers: " + handlers.size());
     }

     public void run()
     {  
        try{    
            while(!done){
                myObject = (ChatMessage)in.readObject();
                System.out.println("Message read: " + myObject.getMessage());
                broadcast();
            }               
        } catch (IOException e){  
            if(e.getMessage().equals("Connection reset")){
                System.out.println("A client terminated its connection.");

            }else{
                System.out.println("Problem receiving: " + e.getMessage());
            }
        }catch(ClassNotFoundException cnfe){
            System.out.println(cnfe.getMessage());
        }finally{
            handlers.remove(this);
        }
     }

Client Code:

    public void actionPerformed(ActionEvent ae){


        //TF ACTION LISTENER
        if (ae.getSource()== tf){

            if (connected) {

                myObject = new ChatMessage();
                myObject.setMessage(tf.getText());
                myObject.setName(name);
                tf.setText("");
                try{
                    myOutputStream.reset();
                    myOutputStream.writeObject(myObject);
                }catch(IOException ioe){
                    System.out.println(ioe.getMessage());
                }
            }else{
                name = tf.getText();
                setName(name);
            }
        }

        //BUTTONS
        if (ae.getSource()== connect){
            connected = true;
            name = tf.getText();
            setName(name);

            try{                        

                scan = new Scanner(System.in);

                myObject = new ChatMessage();

                socketToServer = new Socket("127.0.0.1", 4001);

                myOutputStream = new ObjectOutputStream(socketToServer.getOutputStream());

                myInputStream = new ObjectInputStream(socketToServer.getInputStream());
                start();    
            }
            catch(Exception e){
                System.out.println(e.getMessage()); 
                }

            name = tf.getText();
            setName(name);

            myObject = new ChatMessage();
            myObject.setMessage(getName() + " has connected");

            myObject.setName(name);
            tf.setText("");
            try{
                myOutputStream.reset();
                myOutputStream.writeObject(myObject);
            }catch(IOException ioe){
                System.out.println(ioe.getMessage());
            }           


            disconnect.setEnabled(true);

        }
        if(ae.getSource()== disconnect){
            connected = false;

            myObject = new ChatMessage();
            myObject.setMessage(getName() + " has disconnected");
            myObject.setName(name);
            myObject.setRemoveName(Clientlist);

            tf.setText("");

            try{
                myOutputStream.reset();
                myOutputStream.writeObject(myObject);
            }catch(IOException ioe){
                System.out.println(ioe.getMessage());
            }
            /*
            String[] items = Clientlist.getItems();
            for (String s: items){
            if(s.equals(myObject.getName())){
                RemoveFrom = true; 
                }
            }

            if(RemoveFrom == true){
                Clientlist.remove(myObject.getName());
            }*/

            try{
                myObject.setRemoveName(Clientlist);
                socketToServer.close();
                myOutputStream.close();
                myInputStream.close();


            }catch(IOException ioe){
                System.out.println(ioe.getMessage());
            }   

        }
      }

    public void run(){
        System.out.println("Listening for messages from server . . . ");
        try{
            while(!receivingdone){
                myObject = (ChatMessage)myInputStream.readObject();
                ta.append(myObject.getName() + ": " + myObject.getMessage() + "\n");

                String[] items = Clientlist.getItems();
                boolean inList = false;
                for (String s : items){
                    if (s.equals(myObject.getName())) 
            { inList = true; }
                }
                if (inList == false) {
                    Clientlist.add(myObject.getName());
                }

        if (connected == false){
            for (String s: items){
                if(s.equals(myObject.getName())){
                RemoveFrom = true; 
                    }   
            }

            if(RemoveFrom == true){
                Clientlist.remove(myObject.getName());
            }
        }
            }

    }catch(IOException ioe){
            System.out.println("IOE: " + ioe.getMessage());
        }catch(ClassNotFoundException cnf){
            System.out.println(cnf.getMessage());
        }

    }

Chat Message Code:
    import java.io.*;
import java.awt.*;


public class ChatMessage implements Serializable{
    public String name;
    public String message;
    public List list;


    public ChatMessage(){}
    public ChatMessage(String name, String message){
        setName(name);
        setMessage(message);
        setRemoveName(list);

    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setMessage(String message){
        this.message = message;
    }
    public String getMessage(){
        return message;
    }

    public void setRemoveName(List list){
        list.remove(getName());
        this.list = list;
    }

    public List getRemoveName(){
        return list;    
      } 
    }

0 个答案:

没有答案