将下列数据包通讯程序代码完善,使得可以实现两个客户端之间互相传递信息package ChattingApp; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; @SuppressWarnings("serial") public class ChattingApp extends JFrame implements ActionListener { private JTextArea chatHistory; private JTextField chatInput; private JButton sendBtn, clearBtn, exitBtn; // 发送,清空,退出按钮 private DatagramSocket socket; private InetAddress address; private int port; public ChattingApp() { setTitle("Chat Application"); setSize(500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); chatHistory = new JTextArea(20, 40); chatHistory.setEditable(false); JScrollPane scrollPane = new JScrollPane(chatHistory); chatInput = new JTextField(30); sendBtn = new JButton("发送"); sendBtn.addActionListener(this); clearBtn = new JButton("清空"); clearBtn.addActionListener(this); exitBtn = new JButton("退出"); exitBtn.addActionListener(this); JPanel panel = new JPanel(); panel.add(chatInput); panel.add(sendBtn); panel.add(clearBtn); panel.add(exitBtn); add(scrollPane, BorderLayout.CENTER); add(panel, BorderLayout.SOUTH); setVisible(true); try { socket = new DatagramSocket(); address = InetAddress.getByName("localhost"); port = 9999; } catch (Exception e) { e.printStackTrace(); } new Thread(new ReceiveThread()).start(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == sendBtn) { String message = chatInput.getText(); if (!message.isEmpty()) { byte[] data = message.getBytes(); DatagramPacket packet = new DatagramPacket(data, data.length, address, port); try { socket.send(packet); } catch (Exception ex) { ex.printStackTrace(); } chatInput.setText(""); } } if (e.getSource() == clearBtn) { chatInput.setText(""); } if (e.getSource() == exitBtn) { System.exit(0); } } private class ReceiveThread implements Runnable { public void run() { try { byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); while (true) { socket.receive(packet); String message = new String(packet.getData(), 0, packet.getLength()); chatHistory.append(message + "\n"); } } catch (Exception e) { e.printStackTrace(); } } } }
时间: 2023-06-03 22:03:21 浏览: 107
在该代码基础上,需要新建一个类Client并实现客户端之间的通讯。具体代码实现如下:
package ChattingApp;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
@SuppressWarnings("serial")
public class ChattingApp extends JFrame implements ActionListener {
private JTextArea chatHistory;
private JTextField chatInput;
private JButton sendBtn, clearBtn, exitBtn;// 发送,清空,退出按钮
private DatagramSocket socket;
private InetAddress address;
private int port;
public ChattingApp() {
setTitle("Chat Application");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
chatHistory = new JTextArea(20, 40);
chatHistory.setEditable(false);
JScrollPane scrollPane = new JScrollPane(chatHistory);
chatInput = new JTextField(30);
sendBtn = new JButton("发送");
sendBtn.addActionListener(this);
clearBtn = new JButton("清空");
clearBtn.addActionListener(this);
exitBtn = new JButton("退出");
exitBtn.addActionListener(this);
JPanel panel = new JPanel();
panel.add(chatInput);
panel.add(sendBtn);
panel.add(clearBtn);
panel.add(exitBtn);
add(scrollPane, BorderLayout.CENTER);
add(panel, BorderLayout.SOUTH);
setVisible(true);
try {
socket = new DatagramSocket();
address = InetAddress.getByName("localhost");
port = 9999;
} catch (Exception e) {
e.printStackTrace();
}
new Thread(new ReceiveThread()).start();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sendBtn) {
String message = chatInput.getText();
if (!message.isEmpty()) {
byte[] data = message.getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length, address, port);
try {
socket.send(packet);
} catch (Exception ex) {
ex.printStackTrace();
}
chatInput.setText("");
}
}
if (e.getSource() == clearBtn) {
chatInput.setText("");
}
if (e.getSource() == exitBtn) {
System.exit(0);
}
}
private class ReceiveThread implements Runnable {
public void run() {
try {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
socket.receive(packet);
String message = new String(packet.getData(), 0, packet.getLength());
chatHistory.append(message + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ChattingApp serverApp = new ChattingApp();
Client client1 = new Client("Client 1", "localhost", 9999);
Client client2 = new Client("Client 2", "localhost", 9999);
}
}
class Client extends JFrame implements ActionListener {
private JTextArea chatHistory;
private JTextField chatInput;
private JButton sendBtn, clearBtn, exitBtn;
private DatagramSocket socket;
private InetAddress address;
private int port;
private String name;
public Client(String name, String address, int port) {
this.name = name;
setTitle(name);
setSize(500, 500);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
chatHistory = new JTextArea(20, 40);
chatHistory.setEditable(false);
JScrollPane scrollPane = new JScrollPane(chatHistory);
chatInput = new JTextField(30);
sendBtn = new JButton("发送");
sendBtn.addActionListener(this);
clearBtn = new JButton("清空");
clearBtn.addActionListener(this);
exitBtn = new JButton("退出");
exitBtn.addActionListener(this);
JPanel panel = new JPanel();
panel.add(chatInput);
panel.add(sendBtn);
panel.add(clearBtn);
panel.add(exitBtn);
add(scrollPane, BorderLayout.CENTER);
add(panel, BorderLayout.SOUTH);
setVisible(true);
try {
socket = new DatagramSocket();
this.address = InetAddress.getByName(address);
this.port = port;
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sendBtn) {
String message = chatInput.getText();
if (!message.isEmpty()) {
message = "[" + name + "]: " + message;
byte[] data = message.getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length, address, port);
try {
socket.send(packet);
} catch (Exception ex) {
ex.printStackTrace();
}
chatInput.setText("");
chatHistory.append(message + "\n");
}
}
if (e.getSource() == clearBtn) {
chatInput.setText("");
}
if (e.getSource() == exitBtn) {
dispose();
}
}
}
阅读全文