聊天室管理程序设计(Swing开发)

注意:本项目使用了Gui designer插件开发

Server代码:

import javax.swing.*;
import java.awt.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server {
    public JTextArea Servertext;
    private JButton button1;
    private JButton button2;
    private JLabel JLpanel;
    private JPanel Jpanel;
    private javax.swing.JScrollPane JScrollPane;
    public static List<Socket> onLineSocket = new ArrayList<>();

    public Server() {
        button1.addActionListener(e -> {
            new Thread(() -> {
                try (ServerSocket serverSocket = new ServerSocket(8888)) {
                    SwingUtilities.invokeLater(() -> {
                        Servertext.append("---------服务器已经启动---------" + "\n");
                    });
                    while (true) {
                        Socket socket = serverSocket.accept();
                        onLineSocket.add(socket);
                        SwingUtilities.invokeLater(() -> {
                            Servertext.append(socket.getRemoteSocketAddress() + ":已经加入到聊天室" + "\n");
                        });
                        new Connection(socket, Servertext).start();
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }).start();
        });
        button2.addActionListener(e -> {
            System.exit(0);
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Server");
        frame.setContentPane(new Server().Jpanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setSize(750, 750);
        frame.setVisible(true);
    }

}

Connection代码:

import javax.swing.*;
import java.io.*;
import java.net.Socket;

import static homework.Server.onLineSocket;

public class Connection extends Thread{
    Socket socket;
    JTextArea Servertext;
    public Connection(Socket socket ,JTextArea Servertext) {
        this.socket=socket;
        this.Servertext=Servertext;
    }

    @Override
    public void run() {
        try(InputStream is=socket.getInputStream(); DataInputStream dis=new DataInputStream(is);){
            while(true){

                String msg=(socket.getRemoteSocketAddress()+":" +dis.readUTF());
                Servertext.append(msg+"\n");
                SendMsgAll(msg);
            }


        } catch (Exception e) {
            onLineSocket.remove(socket);
            Servertext.append(socket.getRemoteSocketAddress()+"已经下线"+"\n");
            try {
                SendMsgAll(socket.getRemoteSocketAddress()+"已经下线");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            e.printStackTrace();

        }
    }

    private void SendMsgAll(String msg) throws IOException {
        for (Socket socket:onLineSocket){
            OutputStream os=socket.getOutputStream();
            DataOutputStream dos=new DataOutputStream(os);
            dos.writeUTF(msg);
            dos.flush();
        }
    }
}

Client代码:

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.Socket;

public class Client {
    public Container mainPanel;
    private JTextArea Cilentext;
    private JTextField textField1;
    private JButton Button;
    private JLabel Lable1;
    private JPanel JLabel;
    private javax.swing.JScrollPane JScrollPane;

    private Socket socket;
    private DataOutputStream dos;

    public Client() {
        connectToServer();

        Button.addActionListener(e -> {
            String str = textField1.getText();
            sendMessage(str);
            textField1.setText("");
        });
        receiveMessage();
    }

    private void sendMessage(String msg) {
        new Thread(() -> {
            try {
                if (dos != null) {
                    dos.writeUTF(msg);
                    dos.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    private void connectToServer() {
        Thread thread = new Thread(() -> {
            try {
                socket = new Socket("127.0.0.1", 8888);
                dos = new DataOutputStream(socket.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        thread.setPriority(Thread.MAX_PRIORITY);
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void receiveMessage() {

        new Thread(() -> {
            try {
                InputStream is = socket.getInputStream();
                DataInputStream dis = new DataInputStream(is);
                while (true) {
                    String msg = dis.readUTF();
                    SwingUtilities.invokeLater(() -> Cilentext.append(msg + "\n"));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }


    public static void main(String[] args) {
        JFrame frame = new JFrame("Client");
        frame.setContentPane(new Client().JLabel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setSize(750, 750);
        frame.setVisible(true);
    }

}

注意:使用时必须先打开Server端

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值