JAVA向所有客户发送一个对象

时间:2015-03-09 16:20:49

标签: java object client server send

我尝试聊天。当客户端向服务器发送消息时,它正在工作,服务器接收消息。所以我想向所有客户发送此消息。我尝试了很多东西,但它们没有工作......只是发送消息的客户端,它收到此消息

你可以帮帮我吗? 提前致谢

PS:抱歉我的英文不好

这是控制台中的结果: http://i.stack.imgur.com/VS2wf.png

MainClient

public class MainClient {

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     * @throws java.lang.ClassNotFoundException
     */
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        boolean stop = false;
        Socket socket;
        Scanner nickScan;
        String nick;
        socket = new Socket(InetAddress.getLocalHost(), 2009);

        System.out.println("Hi, what is your name ?");
        nickScan = new Scanner(System.in);
        nick = nickScan.nextLine();

        User u = new User(nick, false, false, true);

        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        oos.writeObject(u);

        EmissionThread e = new EmissionThread(u, socket);
        e.start();

        while(!stop){
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            Message m = (Message)ois.readObject();
            System.out.println(m.getNick() + " : " + m.getMsg());
        }
        //socket.close();//On ferme les connexions

    }
}

MainServer

public class MainServer extends Thread {

    public static void main(String[] args) throws InterruptedException, IOException {
        // TODO code application logic here
        ConnectionThread c = new ConnectionThread();
        c.start();

    }
}

ConnectionThread

public class ConnectionThread extends Thread {

    private static final boolean stop = false;
    Socket socketduserveur;
    ServerSocket socketserver;
    Session s = new Session("#upec");

    public ConnectionThread() throws IOException {
        this.socketserver = new ServerSocket(2009);
    }

    public ServerSocket getSocketserver() {
        return socketserver;
    }


    @Override
    public void run() {

        while (!stop) {
            try {
                socketduserveur = socketserver.accept(); //On accepte les connexions

                ObjectInputStream ois = new ObjectInputStream(socketduserveur.getInputStream());
                User u = (User)ois.readObject();

                System.out.println(u.getNick() + " c'est connecté");

                s.addUserList(u);

                if (s.listAlone()) {
                    System.out.println("Vous etes admin");
                    u.setAdmin(true);
                }

                 ReceptionThread r = new ReceptionThread(socketduserveur);
                 r.start();

            } catch (ClassNotFoundException ex) {
                Logger.getLogger(MainServer.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(ConnectionThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

ReceptionThread

public class ReceptionThread extends Thread {

    private static final boolean stop = false;
    Socket socketduserveur;
    ServerSocket socketserver;

    public ReceptionThread(Socket socketduserveur) {
        this.socketduserveur = socketduserveur;
    }


    @Override
    public void run() {
        while (!stop) {
            try {
                ObjectInputStream ois = new ObjectInputStream(socketduserveur.getInputStream());
                Message m = (Message)ois.readObject();
                System.out.println(m.getNick() + " : " + m.getMsg());


                ObjectOutputStream oos = new ObjectOutputStream(socketduserveur.getOutputStream());
                oos.writeObject(m);

            } catch (IOException | ClassNotFoundException ex) {
                Logger.getLogger(ReceptionThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

EmissionThread

public class EmissionThread extends Thread {

    private User u;
    private Socket socketduserveur;
    private static final boolean stop = false;

    public EmissionThread(User u, Socket socketduserveur) {
        this.u = u;
        this.socketduserveur = socketduserveur;
    }

    @Override
    public void run() {
        while (!stop) {
            try {
                Scanner msgScan;
                String msg;

                msgScan = new Scanner(System.in);
                msg = msgScan.nextLine();
                Message m = new Message(u.getNick(), msg);

                ObjectOutputStream oos = new ObjectOutputStream(socketduserveur.getOutputStream());
                oos.writeObject(m);

            } catch (IOException ex) {
                Logger.getLogger(EmissionThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

消息

public class Message implements Serializable {
    private String nick;
    private String msg;

    public Message(String nick, String msg) {
        this.nick = nick;
        this.msg = msg;
    }

}

会话

public class Session implements Serializable {
    private String name;
    private ArrayList<String> listSession = new ArrayList();
    private ArrayList<User> listUser = new ArrayList();

    public Session(String name) {
        this.name = name;
    }

    public void addSession(String name){
        listSession.add(name);
    }

    public void deleteSession(String name){
        for(String s : listSession){
            if(name.equals(s)){
                listSession.remove(s);
            }
        }
    }

    public boolean existSession(String name){
        for(String s : listSession){
            if(name.equals(s)){
                return true;
            }
        }
        return false;
    }


    public void addUserList(User u){
        listUser.add(u);
    }

    public boolean listAlone(){
        int compteur = 0;
        for(User u : listUser){
            compteur++;
        }
        return compteur == 1;
    }

}

用户

public class User implements Serializable {
    private String nick;
    private final Session session;
    private boolean admin, moderator, voice;


    public User(String nick, boolean admin, boolean moderator, boolean voice) {
        this.nick = nick;
        this.admin = admin;
        this.moderator = moderator;
        this.voice = voice;
        this.session = new Session("#upec");
    }

}

1 个答案:

答案 0 :(得分:0)

您可以在tomcat上使用websockets。如果您下载tomcat,则已经建立了一个聊天应用程序作为示例