用Java实现一个网络监听程序,要求如下: 1、使用图形用户界面。有服务器和客户端,服务器监听客户端的登陆。即:如果A客户端登陆服务器,则服务器界面上的A字母对应的红灯变成绿色。绿灯表示连接,红灯表示断线。 2、可以通过图形用户界面添加新的客户机。 3、可以通过服务器进行广播 4、添加注释
时间: 2024-02-05 11:13:47 浏览: 25
以下是一个简单的Java网络监听程序的实现,满足上述要求:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class NetworkMonitor extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private static final int PORT = 1234; // 监听端口
private JLabel serverStatus; // 服务器状态
private JButton addButton; // 添加客户端按钮
private JButton broadcastButton; // 广播按钮
private JPanel clientPanel; // 客户端面板
private ArrayList<ClientHandler> clients; // 客户端列表
public NetworkMonitor() {
super("网络监听程序");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 服务器状态标签
serverStatus = new JLabel("服务器已启动");
add(serverStatus, BorderLayout.NORTH);
// 添加客户端按钮
addButton = new JButton("添加客户端");
addButton.addActionListener(this);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
buttonPanel.add(addButton);
add(buttonPanel, BorderLayout.SOUTH);
// 客户端面板
clientPanel = new JPanel();
add(clientPanel, BorderLayout.CENTER);
// 广播按钮
broadcastButton = new JButton("广播");
broadcastButton.addActionListener(this);
add(broadcastButton, BorderLayout.EAST);
// 客户端列表
clients = new ArrayList<ClientHandler>();
setVisible(true);
// 启动服务器
startServer();
}
/**
* 启动服务器
*/
private void startServer() {
try {
ServerSocket serverSocket = new ServerSocket(PORT);
while (true) {
Socket socket = serverSocket.accept();
ClientHandler clientHandler = new ClientHandler(socket);
clients.add(clientHandler);
clientPanel.add(clientHandler.getClientPanel());
clientPanel.revalidate();
clientPanel.repaint();
serverStatus.setText("客户端已连接:" + clients.size());
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 处理按钮点击事件
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
addClient();
} else if (e.getSource() == broadcastButton) {
broadcast();
}
}
/**
* 添加客户端
*/
private void addClient() {
ClientDialog dialog = new ClientDialog(this);
if (dialog.showDialog()) {
ClientHandler clientHandler = new ClientHandler(dialog.getIP(), dialog.getPort());
clients.add(clientHandler);
clientPanel.add(clientHandler.getClientPanel());
clientPanel.revalidate();
clientPanel.repaint();
serverStatus.setText("客户端已连接:" + clients.size());
}
}
/**
* 广播消息
*/
private void broadcast() {
for (ClientHandler clientHandler : clients) {
clientHandler.sendMessage("服务器广播消息");
}
}
/**
* 客户端处理类
*/
private class ClientHandler {
private Socket socket; // 客户端socket
private JLabel nameLabel; // 客户端名称
private JLabel statusLabel; // 客户端连接状态
public ClientHandler(Socket socket) {
this.socket = socket;
nameLabel = new JLabel(socket.getInetAddress().getHostAddress());
statusLabel = new JLabel("连接中");
statusLabel.setForeground(Color.RED);
}
public ClientHandler(String ip, int port) {
try {
socket = new Socket(ip, port);
nameLabel = new JLabel(ip + ":" + port);
statusLabel = new JLabel("连接中");
statusLabel.setForeground(Color.RED);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取客户端面板
*/
public JPanel getClientPanel() {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel.add(nameLabel);
panel.add(statusLabel);
return panel;
}
/**
* 发送消息
*/
public void sendMessage(String message) {
try {
socket.getOutputStream().write(message.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 添加客户端对话框类
*/
private class ClientDialog extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JLabel ipLabel;
private JLabel portLabel;
private JTextField ipField;
private JTextField portField;
private JButton okButton;
private JButton cancelButton;
private boolean result;
public ClientDialog(JFrame parent) {
super("添加客户端");
setLocationRelativeTo(parent);
setSize(300, 150);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
ipLabel = new JLabel("IP地址:");
portLabel = new JLabel("端口号:");
ipField = new JTextField(20);
portField = new JTextField(20);
okButton = new JButton("确定");
okButton.addActionListener(this);
cancelButton = new JButton("取消");
cancelButton.addActionListener(this);
JPanel panel = new JPanel(new BorderLayout());
JPanel inputPanel = new JPanel(new GridLayout(2, 2));
inputPanel.add(ipLabel);
inputPanel.add(ipField);
inputPanel.add(portLabel);
inputPanel.add(portField);
panel.add(inputPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
panel.add(buttonPanel, BorderLayout.SOUTH);
add(panel);
result = false;
}
/**
* 显示对话框
*/
public boolean showDialog() {
setVisible(true);
return result;
}
/**
* 获取IP地址
*/
public String getIP() {
return ipField.getText();
}
/**
* 获取端口号
*/
public int getPort() {
return Integer.parseInt(portField.getText());
}
/**
* 处理按钮点击事件
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton) {
result = true;
dispose();
} else if (e.getSource() == cancelButton) {
result = false;
dispose();
}
}
}
public static void main(String[] args) {
new NetworkMonitor();
}
}
```
该程序使用Java Swing构建图形用户界面,包括服务器状态标签、添加客户端按钮、客户端面板和广播按钮。通过监听端口来接受客户端连接,每个客户端连接后在客户端面板中添加一个标签,显示客户端的IP地址和连接状态。点击添加客户端按钮可以手动添加客户端,点击广播按钮可以向所有客户端发送广播消息。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)