```服务器发送数据过去给客户端了,客户端的表格里面没数据
客户端代码
package server;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ClientTest2 {
private JFrame frame;
private JButton connectButton;
private JButton sendButton;
private JTextArea logArea;
private Socket socket;
private JTextField inputField;
private JTable table;
private DefaultTableModel tableModel;
private int lineCount = 0; // 用于计数接收到的行数
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Client());
}
// private static Map<String, String> airportCodes = new HashMap<>();
private static Map<String, String> aircompanyCodes = new HashMap<>();
private static Map<String, String> flightStates = new HashMap<>();
static final String[] columnNames = {"行号", "航空公司", "航班号", "目的站/经停站", "开始值机时间", "结束值机时间", "开始登机时间", "结束登机时间", "航班状态", "航站楼", "登机口", "起飞时间", "预计到达时间"};
static {
loadAirportCodes("G:\\eclipse\\.metadata\\FinalHomework\\city.txt");
loadAirportCodes("G:\\eclipse\\.metadata\\FinalHomework\\company.txt");
loadFlightStates("G:\\eclipse\\.metadata\\FinalHomework\\airportstate.txt"); // 加载航班状态
}
private static void loadFlightStates(String filename) {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 2) {
flightStates.put(parts[0], parts[1]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public ClientTest2() {
frame = new JFrame("Flight Client");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 500);
frame.setLayout(new BorderLayout());
createAndAddComponents();
frame.setVisible(true);
}
private static void loadAirportCodes(String filename) {
// TODO: Load airport codes from file
}
private void createAndAddComponents() {
inputField = new JTextField();
frame.add(inputField, BorderLayout.SOUTH);
connectButton = new JButton("Connect to Server");
connectButton.addActionListener(e -> connectToServer());
frame.add(connectButton, BorderLayout.NORTH);
sendButton = new JButton("Send Data");
sendButton.setEnabled(false);
sendButton.addActionListener(e -> sendData());
frame.add(sendButton, BorderLayout.SOUTH);
// 初始化 tableModel 并设置为 table 的模型
tableModel = new DefaultTableModel(columnNames, 0);
table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
logArea = new JTextArea();
logArea.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(logArea);
frame.add(logScrollPane, BorderLayout.EAST);
}
private void connectToServer() {
try {
socket = new Socket("localhost", 5000);
Map<String, ClientHandler.FlightInfo> flightData = new HashMap<>();
Map<PrintWriter, ClientHandler> clientHandlers = new HashMap<>();
new Thread(new ClientHandler(socket, tableModel, flightData, clientHandlers)).start();
logArea.append("Connected to server.\n");
sendButton.setEnabled(true);
// 显示连接成功的提示弹窗
JOptionPane.showMessageDialog(frame, "Connection to server successful!", "Connection Status", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException ex) {
logArea.append("Failed to connect to server: " + ex.getMessage() + "\n");
sendButton.setEnabled(false);
// 显示连接失败的提示弹窗
JOptionPane.showMessageDialog(frame, "Failed to connect to server: " + ex.getMessage(), "Connection Status", JOptionPane.ERROR_MESSAGE);
}
}
private void sendData() {
if (socket != null && socket.isConnected()) {
try {
String dataToSend = inputField.getText();
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(dataToSend);
out.flush();
logArea.append("Sent: " + dataToSend + "\n");
} catch (IOException e) {
logArea.append("Error sending data: " + e.getMessage() + "\n");
}
} else {
logArea.append("Cannot send data: Not connected to the server.\n");
}
}
class ClientHandler implements Runnable {
private Socket clientSocket;
private BufferedReader in;
private PrintWriter out;
private DefaultTableModel logModel;
private Map<String, FlightInfo> flightData;
private Map<PrintWriter, ClientHandler> clientHandler;
public ClientHandler(Socket socket, DefaultTableModel logModel, Map<String, FlightInfo> flightData, Map<PrintWriter, ClientHandler> clientHandlers) {
this.clientSocket = socket;
this.logModel = logModel;
this.flightData = flightData;
this.clientHandler = clientHandlers;
try {
this.in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
this.out = new PrintWriter(clientSocket.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {//处理从服务器接收到的数据
try {
clientHandler.put(out, this);
ByteBuffer buffer = ByteBuffer.allocate(1024);//存储从服务器接收的数据
String inputLine;
lineCount = 0; // 行数计数器
while ((inputLine = in.readLine()) != null) {//循环读取每一行数据
FlightInfo flightInfo = parseInputLine(inputLine);// 解析接收到的行数据,将其转换为 FlightInfo 对象
if (flightInfo != null) {
// 如果解析成功,将 FlightInfo 对象添加到 flightData 映射中
flightData.put(flightInfo.flightNumber, flightInfo);
// flightData.put(inputLine, flightInfo);
addFlightInfo(flightInfo);
// 将 FlightInfo 对象添加到表格模型中,以便在 GUI 中显示
}
lineCount++; // 增加行数计数
SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(frame, "Received line " + lineCount, "Information", JOptionPane.INFORMATION_MESSAGE));
if ("EOF".equals(inputLine.trim())) {
// 如果接收到 EOF 标记,表示数据接收完成,退出循环
break;}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
clientSocket.close();
in.close();
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
sendButton.setEnabled(false);
}
}
public static class FlightInfo {
String aircompany;
String flightNumber;
String destination;
String departureTime;
String arrivalTime;
String StartBoardingTime;
String EndBoardingTime;
String StartCheckin;
String EndCheckin;
String state;
String gate;
String TerminalNum;
}
private FlightInfo parseInputLine(String inputLine) {
FlightInfo flightInfo = new FlightInfo();
// 匹配航空公司
Pattern aircompanyPattern = Pattern.compile("awcd=(\\d{1,4})");
Matcher aircompanyMatcher = aircompanyPattern.matcher(inputLine);
if (aircompanyMatcher.find()) {
flightInfo.aircompany = aircompanyCodes.getOrDefault(aircompanyMatcher.group(1), "Unknown");
}
System.out.println(flightInfo.aircompany);
// 匹配航班号
Pattern flightNumberPattern = Pattern.compile("ffid=(\\w+-\\d+-\\d{8}-[A-Z])");
Matcher flightNumberMatcher = flightNumberPattern.matcher(inputLine);
if (flightNumberMatcher.find()) {
flightInfo.flightNumber = flightNumberMatcher.group(1).split("-")[1];
}
System.out.println(flightInfo.flightNumber);
// 匹配目的地
Pattern destinationPattern = Pattern.compile("(?<=apcd=)[\\w]{3}(?=,)");
Matcher destinationMatcher = destinationPattern.matcher(inputLine);
while (destinationMatcher.find()) {
String destination = destinationMatcher.group(0).trim();
if (flightInfo.destination == null) {
flightInfo.destination = destination;
} else {
flightInfo.destination += ", " + destination;
}
}
System.out.println(flightInfo.destination);
// 匹配起飞时间
Pattern departureTimePattern = Pattern.compile("(?<=fptt=)[\\d]{14}(?=,)");
Matcher departureTimeMatcher = departureTimePattern.matcher(inputLine);
if (departureTimeMatcher.find()) {
flightInfo.departureTime = departureTimeMatcher.group(0).trim();
}
System.out.println(flightInfo.departureTime);
// 匹配预计到达时间
Pattern arrivalTimePattern = Pattern.compile("fett=(\\d{14})");
Matcher arrivalTimeMatcher = arrivalTimePattern.matcher(inputLine);
if (arrivalTimeMatcher.find()) {
flightInfo.arrivalTime = arrivalTimeMatcher.group(1).trim();
}
System.out.println(flightInfo.arrivalTime);
// 匹配航班状态
Pattern statePattern = Pattern.compile("(?<=stat=)[\\w]{3}(?=,)");
Matcher stateMatcher = statePattern.matcher(inputLine);
if (stateMatcher.find()) {
flightInfo.state = stateMatcher.group(0).trim();
}
System.out.println(flightInfo.state);
// 匹配登机口
Pattern gatePattern = Pattern.compile("(?<=code=)[\\d]{3}(?=,)");
Matcher gateMatcher = gatePattern.matcher(inputLine);
if (gateMatcher.find()) {
flightInfo.gate = gateMatcher.group(0).trim();
}
System.out.println(flightInfo.gate);
// 匹配航站楼
Pattern terminalPattern = Pattern.compile("(?<=ter=)[\\d]{1,2}(?=,)");
Matcher terminalMatcher = terminalPattern.matcher(inputLine);
if (terminalMatcher.find()) {
flightInfo.TerminalNum = terminalMatcher.group(0).trim();
}
System.out.println(flightInfo.TerminalNum);
// 匹配开始值机时间
Pattern startCheckinPattern = Pattern.compile("(?<=scin=)[\\d]{14}(?=,)");
Matcher startCheckinMatcher = startCheckinPattern.matcher(inputLine);
if (startCheckinMatcher.find()) {
flightInfo.StartCheckin = startCheckinMatcher.group(0).trim();
}
System.out.println(flightInfo.StartCheckin);
//匹配结束值机时间
Pattern endCheckinPattern = Pattern.compile("(?<=ecin=)[\\d]{14}(?=,)");
Matcher endCheckinMatcher = endCheckinPattern.matcher(inputLine);
if (endCheckinMatcher.find()) {
flightInfo.EndCheckin = endCheckinMatcher.group(0).trim();
}
System.out.println(flightInfo.EndCheckin);
//匹配开始登机时间
Pattern startBoardingPattern = Pattern.compile("(?<=sbor=)[\\d]{14}(?=,)");
Matcher startBoardingMatcher = startBoardingPattern.matcher(inputLine);
if (startBoardingMatcher.find()) {
flightInfo.StartBoardingTime = startBoardingMatcher.group(0).trim();
}
System.out.println(flightInfo.StartBoardingTime);
//匹配结束登机时间
Pattern endBoardingPattern = Pattern.compile("(?<=ebor=)[\\d]{14}(?=,)");
Matcher endBoardingMatcher = endBoardingPattern.matcher(inputLine);
if (endBoardingMatcher.find()) {
flightInfo.EndBoardingTime = endBoardingMatcher.group(0).trim();
}
System.out.println(flightInfo.EndBoardingTime);
// System.out.println("no data!");
return flightInfo;
}
public void addFlightInfo(FlightInfo flightInfo) {
//添加数据到表格
Object[] rowData = new Object[columnNames.length];
for(int i = 0;i < columnNames.length; i++) {
switch(columnNames[i]) {
case "航空公司":
rowData[i] = flightInfo.aircompany;
break;
case "航班号":
rowData[i] = flightInfo.flightNumber;
break;
case "目的站":
rowData[i] = flightInfo.destination;
break;
case "开始值机时间":
rowData[i] = flightInfo.StartCheckin;
break;
case "结束值机时间":
rowData[i] = flightInfo.EndCheckin;
break;
case "开始登机时间":
rowData[i] = flightInfo.StartBoardingTime;
break;
case "结束登机时间":
rowData[i] = flightInfo.EndBoardingTime;
break;
case "航班状态":
rowData[i] = flightInfo.state;
break;
case "航站楼":
rowData[i] = flightInfo.TerminalNum;
break;
case "登机口":
rowData[i] = flightInfo.gate;
break;
case "起飞时间":
rowData[i] = flightInfo.departureTime;
break;
case "预计到达时间":
rowData[i] = flightInfo.arrivalTime;
break;
default:
rowData[i] = "";
}
}
tableModel.addRow(rowData);
}
// private void parseAndAddToTable(String inputLine) {
// if (inputLine.trim().isEmpty()) return;
// FlightInfo flightInfo = new FlightInfo();
// if (isComplete(flightInfo)) { //检查完整性,如果完整,则添加到Table里面
// addFlightInfo(flightInfo);
// }
// }
// private boolean isComplete(FlightInfo flightInfo) {
// return flightInfo.aircompany!= null && flightInfo.flightNumber!= null
// && flightInfo.destination!= null && flightInfo.StartCheckin!=null&& flightInfo.EndCheckin!=null&& flightInfo.StartBoardingTime!=null&& flightInfo.EndBoardingTime!=null&& flightInfo.state!=null&& flightInfo.gate!=null&& flightInfo.departureTime!= null
// && flightInfo.state!= null && flightInfo.gate!= null
// && flightInfo.TerminalNum!= null && flightInfo.arrivalTime!= null;
// }
//
// }
//
}
}
服务器代码
package server;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
public class ServerTest {
private JFrame frame;
private JButton startButton, stopButton, sendButton;
private JTextArea messageArea; // 用于显示客户端消息的文本区域
private ServerSocket serverSocket;
private Thread serverThread;
private Socket clientSocket;
private PrintWriter out;
public ServerTest() {
frame = new JFrame("TCP Server");
frame.setSize(300, 400); // 增加高度以适应新添加的文本区域
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
startButton = new JButton("Start Server");
startButton.addActionListener(e -> startServer());
stopButton = new JButton("Stop Server");
stopButton.addActionListener(e -> stopServer());
sendButton = new JButton("Send Data");
sendButton.addActionListener(e -> sendData());
sendButton.setEnabled(false); // Disable initially
messageArea = new JTextArea(10, 30); // 创建文本区域,10行30列
messageArea.setEditable(false); // 设置为不可编辑
JScrollPane scrollPane = new JScrollPane(messageArea);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER); // 添加滚动面板
frame.getContentPane().add(startButton, BorderLayout.NORTH);
frame.getContentPane().add(stopButton, BorderLayout.SOUTH);
frame.getContentPane().add(sendButton, BorderLayout.EAST);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
stopServer();
}
});
frame.setVisible(true);
}
public void startServer() {
serverThread = new Thread(() -> {
try {
ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.bind(new InetSocketAddress(5000));
System.out.println("Server started on port 5000");
SocketChannel clientChannel = serverChannel.accept();
System.out.println("Client connected.");
// 确保客户端已连接
if (clientChannel != null) {
out = new PrintWriter(Channels.newOutputStream(clientChannel), true);
sendButton.setEnabled(true); // Enable send button after connection
// new Thread(new ClientHandler(clientChannel)).start();
try (BufferedReader fileReader = new BufferedReader(new FileReader("G:\\eclipse\\.metadata\\FinalHomework\\FDSdata.txt"))) {
String line;
while ((line = fileReader.readLine()) != null) { // 循环读取每一行
out.println(line);
out.flush(); // 确保数据立即发送
}
SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(frame, "All data sent successfully!", "Success", JOptionPane.INFORMATION_MESSAGE));
} catch (IOException e) {
SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(frame, "Error sending data: " + e.getMessage(), "IO Error", JOptionPane.ERROR_MESSAGE));
}
}
} catch (IOException e) {
System.err.println("Server exception: " + e.getMessage());
e.printStackTrace();
}
});
serverThread.start();
}
public void stopServer() {
if (serverSocket != null) {
try {
serverSocket.close();
System.out.println("Server stopped.");
try {
long lastActiveTime = 0;
// 检查客户端是否超过一定时间未活跃
if (System.currentTimeMillis() - lastActiveTime > 60000) { // 假设60秒无活动则断开连接
clientSocket.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
} catch (IOException e) {
System.err.println("Error stopping server: " + e.getMessage());
e.printStackTrace();
}
}
if (serverThread != null) {
serverThread.interrupt();
}
frame.dispose();
}
public void sendData() {
// if (clientSocket == null || !clientSocket.isConnected()) {
// SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(frame, "Not connected to a client!", "Error", JOptionPane.ERROR_MESSAGE));
// return;
// }
new Thread(() -> {
try (BufferedReader fileReader = new BufferedReader(new FileReader("G:\\eclipse\\.metadata\\FinalHomework\\FDSdata.txt"))) {
String line;
while ((line = fileReader.readLine()) != null) { // 循环读取每一行
out.println(line);
out.flush(); // 确保数据立即发送
}
SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(frame, "All data sent successfully!", "Success", JOptionPane.INFORMATION_MESSAGE));
} catch (IOException e) {
SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(frame, "Error sending data: " + e.getMessage(), "IO Error", JOptionPane.ERROR_MESSAGE));
}
}).start();
out.println("EOF");
}
class ClientHandler implements Runnable {
private SocketChannel socketChannel;
private ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024); // 假设最大消息大小为4KB
public ClientHandler(SocketChannel socketChannel) {
this.socketChannel = socketChannel;
}
public void run() {
while (true) {
try {
int bytesRead = socketChannel.read(buffer);
if (bytesRead == -1) { // EOF
break;
}
buffer.flip();
byte[] data = new byte[buffer.limit()];
buffer.get(data);
String message = new String(data, StandardCharsets.UTF_8);
displayMessage(message);
buffer.clear();
} catch (IOException e) {
System.err.println("Client exception: " + e.getMessage());
e.printStackTrace();
break;
}
}
try {
if (socketChannel != null) socketChannel.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void displayMessage(String message) {
SwingUtilities.invokeLater(() -> messageArea.append(message + "\n"));
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(ServerTest::new);
}
}**