import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
@SuppressWarnings("all")
public class MyServer {
//保存接收信息的端口号
static ArrayList<Integer> list = new ArrayList<>();
// 保存接收文件的端口号
static ArrayList<Integer> clientServer = new ArrayList<>();
// 保存添加用户的用户名
static ArrayList<String> clientName = new ArrayList<>();
// 分别是接收信息端口号、接收文件端口号
static int CLIEN_TPORT, CLIENT_SERVER;
// 客户端名称
static String CLIENT_NAME;
public static void main(String[] args) throws IOException {
serverFrame();
}
//初始化服务器界面
public static void serverFrame() {
//界面
JFrame jf = new JFrame("服务器");
jf.setSize(600, 500);//窗体大小
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗体关闭方式
jf.setLocationRelativeTo(null);// 居中
jf.setLayout(new FlowLayout(FlowLayout.CENTER));
// 创建组件对象
JLabel j1 = new JLabel("用户端口:\n");
JTextField t1 = new JTextField(7);// 33个字符宽
JLabel j2 = new JLabel("用户名称:");
JTextField t2 = new JTextField(7);// 33个字符宽
JLabel j3 = new JLabel("服务端口:");
JTextField t3 = new JTextField(7);// 33个字符宽
JButton b1 = new JButton("添加新用户");
JButton b2 = new JButton("启动服务器");
JButton b3 = new JButton("查看在线用户");
JTextArea area1 = new JTextArea(24, 55);
// 处理点击启动服务器按钮的事件
new Thread(new Runnable() {
@Override
public void run() {
b2.addActionListener(e -> {
area1.append("启动服务器成功!\n");
b2.setEnabled(false);
new Thread(new Runnable() {
@Override
public void run() {
serverConnect1();
}
}).start();
});
}
}).start();
// 处理点击按钮添加用户的事件
new Thread(new Runnable() {
@Override
public void run() {
b1.addActionListener(e -> {
if (t1.getText().trim().isEmpty() || t2.getText().trim().isEmpty() || t3.getText().trim().isEmpty()) {
return;
}
area1.append("添加用户\"" + t2.getText() + "\"成功!\n");
CLIEN_TPORT = Integer.parseInt(t1.getText());
CLIENT_SERVER = Integer.parseInt(t3.getText());
CLIENT_NAME = t2.getText();
list.add(CLIEN_TPORT);
clientServer.add(CLIENT_SERVER);
clientName.add(CLIENT_NAME);
new Thread(new Runnable() {
@Override
public void run() {
try {
addClient(CLIENT_NAME, CLIEN_TPORT, CLIENT_SERVER, 300, 300);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}).start();
t2.setText("");
t1.setText("");
t3.setText("");
});
}
}).start();
// 查看用户列表
new Thread(new Runnable() {
@Override
public void run() {
b3.addActionListener(e -> {
for (String s : clientName) {
area1.append("\"" + s + "\"在线中...\n");
}
});
}
}).start();
jf.add(j1);
jf.add(t1);
jf.add(j2);
jf.add(t2);
jf.add(j3);
jf.add(t3);
jf.add(b1);
jf.add(b2);
jf.add(b3);
jf.add(area1);
jf.setVisible(true);
}
public static void serverConnect1() {
//监听发送信息的线程
new Thread(() -> {
// 服务器监听
ServerSocket serverSocket1 = null;
try {
serverSocket1 = new ServerSocket(8888);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
String[] msg = {""};
Socket socket = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
socket = serverSocket1.accept();
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
assert br != null;
msg[0] = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
//接收到某个客户端请求之后开启线程进行分别转发
for (int i = 0; i < list.size(); i++) {
int finalI = i;
new Thread(() -> {
try {
Socket socket1 = new Socket("localhost", list.get(finalI));
System.out.println(CLIEN_TPORT);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket1.getOutputStream()));
bufferedWriter.write(msg[0]);
bufferedWriter.newLine();
bufferedWriter.flush();
bufferedWriter.close();
socket1.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}).start();
}
try {
bw.close();
br.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
//转发来自客户端的文件再转发回给客户端
new Thread(() -> {
ServerSocket serverSocket = null;
byte[] bytes = null;
try {
serverSocket = new ServerSocket(9999);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
Socket socket = null;
try {
System.out.println("ok");
socket = serverSocket.accept();
System.out.println("hh");
bytes = StreamUtils.streamToByteArray(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
try {
byte[] finalBytes = bytes;
System.out.println(finalBytes);
// 将文件转发给用户
for (int i = 0; i < clientServer.size(); i++) {
int finalI = i;
ne