Java Multicast Socket“离开聊天”实现

时间:2019-03-17 12:21:10

标签: java sockets multicast messenger groupchat

在实施退出聊天机制时遇到问题。问题是,当一个用户在控制台中键入close以退出群聊时,程序结束并返回0,但同时其他所有用户(并行运行的程序)都被“踢了”,我的意思是,其他用户的程序仍在运行,但它们不发送消息。简单的socket.leaveGroup(group)不起作用:(

这是我的代码,您可以尝试一下以查找发生的情况。非常感谢您的帮助。

public class GroupChat {
    static volatile boolean finished = false;
    static String name;
    static ArrayList<String> groups;
    static String myRoom;

    GroupChat() {
        groups = new ArrayList<>();
        groups.add("Room 1:");
        groups.add("Room 2:");
        groups.add("Room 3:");
        groups.add("Room 4:");
    }

    public static void main(String[] args) {
        if (args.length != 2) {
            System.out.println("Need IP address and port...");
        } else {

            try {
                Scanner sc = new Scanner(System.in);
                int getRoom;

                System.out.print("Enter group chat, press (1), (2), (3) or (4) ... ");
                getRoom = Integer.parseInt(sc.nextLine());
                System.out.print("Enter username: ");
                name = sc.nextLine();

                GroupChat GC = new GroupChat();
                myRoom = groups.get(getRoom - 1);

                InetAddress group = InetAddress.getByName(args[0]);
                int port = Integer.parseInt(args[1]);

                MulticastSocket socket = new MulticastSocket(port);
                socket.setTimeToLive(0);
                socket.joinGroup(group);

                Thread t = new Thread(new ReadThread(socket, group, port));
                t.start();

                String message = myRoom + " " + name + " has joined.";
                byte[] buffer = message.getBytes();
                DatagramPacket datagram = new
                        DatagramPacket(buffer, buffer.length, group, port);
                socket.send(datagram);

                System.out.println("Wpisz wiadomość ...\n");

                message = "";
                String closeThis = message;
                while (!closeThis.equals("close")) {
                    Scanner MyMessage = new Scanner(System.in);
                    message = MyMessage.nextLine();
                    closeThis = message;

                    message = myRoom + " " + name + ": " + message;
                    buffer = message.getBytes();
                    datagram = new
                            DatagramPacket(buffer, buffer.length, group, port);
                    socket.send(datagram);
                }

                message = myRoom + name + " has left.";
                buffer = message.getBytes();
                datagram = new
                        DatagramPacket(buffer, buffer.length, group, port);
                socket.send(datagram);

                socket.leaveGroup(group);
                socket.close();

            } catch (SocketException se) {
                System.out.println("Error creating socket");
                se.printStackTrace();
            } catch (IOException ie) {
                System.out.println("Error reading/writing from/to socket");
                ie.printStackTrace();
            }
        }
    }
}

class ReadThread implements Runnable {
    private MulticastSocket socket;
    private InetAddress group;
    private int port;
    private static final int MAX_LEN = 1000;

    ReadThread(MulticastSocket socket, InetAddress group, int port) {
        this.socket = socket;
        this.group = group;
        this.port = port;
    }

    @Override
    public void run() {
        String message = "";
        boolean close = false;
        while (!close) {
            byte[] buffer = new byte[ReadThread.MAX_LEN];
            DatagramPacket datagram = new
                    DatagramPacket(buffer, buffer.length, group, port);

            try {
                socket.receive(datagram);
                message = new String(buffer, 0, datagram.getLength(), "UTF-8");

                if(message.substring(message.length() - 5).equals("close")) {
                    close = true;
                }
                else {
                    if (message.startsWith(GroupChat.myRoom)) {
                        message = message.substring(8);
                        if (!message.startsWith(GroupChat.name)) {
                            System.out.println(message);
                        }
                    }
                }
            } catch (IOException e) {
                System.out.println("Socket closed!");
            }
        }
        System.out.println(GroupChat.name + " has left.");
    }
}

0 个答案:

没有答案