注意:本项目使用了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端