运用Java编写一个GUI界面的Socket聊天室(UDPChatGUl.java) 具体要求: (2)用Socket编程实现服务器端和客户端程序的编写。 (3)服务器端只负责好友列表的刷新,不负责客户端聊天信息的转发。
时间: 2024-02-21 16:57:17 浏览: 89
好的,以下是一个简单的UDP聊天室GUI界面程序的实现,您可以参考一下:
UDPChatGUl.java:
```java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class UDPChatGUl extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextArea textArea;
private JList<String> list;
private DefaultListModel<String> listModel;
private Map<String, String> friendMap;
private DatagramSocket socket;
private DatagramPacket packet;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UDPChatGUl frame = new UDPChatGUl();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public UDPChatGUl() throws SocketException {
setTitle("UDP聊天室");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(10, 227, 313, 23);
contentPane.add(textField);
textField.setColumns(10);
JButton sendButton = new JButton("发送");
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
send();
}
});
sendButton.setBounds(333, 227, 91, 23);
contentPane.add(sendButton);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 10, 313, 206);
contentPane.add(scrollPane);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
listModel = new DefaultListModel<String>();
list = new JList<String>(listModel);
list.setBounds(333, 10, 91, 206);
contentPane.add(list);
friendMap = new HashMap<String, String>();
// 创建服务器端socket
socket = new DatagramSocket(12345);
// 启动接收线程
new Thread(new Runnable() {
public void run() {
receive();
}
}).start();
// 向服务器端请求好友列表
requestFriendList();
}
// 发送消息
private void send() {
String message = textField.getText();
if (message != null && !message.trim().equals("")) {
String friend = list.getSelectedValue();
if (friend != null) {
String friendAddress = friendMap.get(friend);
if (friendAddress != null) {
try {
byte[] data = message.getBytes();
InetAddress address = InetAddress.getByName(friendAddress.split(":")[0]);
int port = Integer.parseInt(friendAddress.split(":")[1]);
DatagramPacket packet = new DatagramPacket(data, data.length, address, port);
socket.send(packet);
textArea.append("我对" + friend + "说:" + message + "\n");
textField.setText("");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
// 接收消息
private void receive() {
while (true) {
try {
byte[] data = new byte[1024];
packet = new DatagramPacket(data, data.length);
socket.receive(packet);
String message = new String(packet.getData(), 0, packet.getLength());
String friend = getFriend(packet.getAddress().getHostAddress(), packet.getPort());
textArea.append(friend + "对我说:" + message + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 向服务器端请求好友列表
private void requestFriendList() {
try {
byte[] data = "list".getBytes();
InetAddress address = InetAddress.getByName("localhost");
int port = 12345;
DatagramPacket packet = new DatagramPacket(data, data.length, address, port);
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
// 处理好友列表信息
private void handleFriendList(String message) {
String[] friends = message.split(",");
for (String friend : friends) {
String[] info = friend.split(":");
String name = info[0];
String address = info[1] + ":" + info[2];
friendMap.put(name, address);
listModel.addElement(name);
}
}
// 获取好友信息
private String getFriend(String hostAddress, int port) {
for (Map.Entry<String, String> entry : friendMap.entrySet()) {
String value = entry.getValue();
String[] info = value.split(":");
if (info[0].equals(hostAddress) && info[1].equals(String.valueOf(port))) {
return entry.getKey();
}
}
return null;
}
}
```
上述代码中,我们使用了Java的Swing库来实现GUI界面。程序分为客户端和服务器端两部分,其中,客户端负责向服务器端请求好友列表、发送消息和接收消息,而服务器端只负责维护好友列表并定时向客户端广播好友列表信息。
在实现中,我们使用了UDP协议进行通信。客户端向服务器端发送请求获取好友列表的信息,服务器端在收到请求后会将好友列表信息发送给客户端。客户端接收到好友列表信息后将其显示在GUI界面的好友列表中。客户端向指定好友发送消息时,需要获取该好友的IP地址和端口号,并通过UDP连接向该好友发送消息。当客户端接收到其他好友发送的消息时,需要在聊天窗口中显示该消息。
再次提醒,这是一个简单的UDP聊天室GUI界面程序的实现,具体的实现细节和代码,需要根据您的具体需求和环境进行调整。
阅读全文