用eclipse写一个简单聊天程序并给出代码
时间: 2024-01-01 22:04:21 浏览: 76
好的,下面是一个简单的聊天程序的示例代码。这个程序分为服务器端和客户端两部分,你需要在 Eclipse 中创建两个项目分别对应这两部分。
服务器端代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Server {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(8888);
System.out.println("Server started. Waiting for clients...");
Socket socket = server.accept();
System.out.println("Client connected: " + socket.getInetAddress().getHostAddress());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
while (true) {
String message = in.readLine();
if (message == null) {
break;
}
System.out.println("Received message: " + message);
// 获取当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(new Date());
// 发送消息给客户端
out.println(time + " Server: " + message);
}
in.close();
out.close();
socket.close();
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
客户端代码:
```java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
public class Client {
private static final String SERVER_IP = "localhost";
private static final int SERVER_PORT = 8888;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private JFrame frame;
private JTextArea messageArea;
private JTextField inputField;
private JButton sendButton;
public Client() {
// 创建界面
frame = new JFrame("Chat Room");
JPanel panel = new JPanel(new BorderLayout());
messageArea = new JTextArea();
messageArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(messageArea);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
panel.add(scrollPane, BorderLayout.CENTER);
inputField = new JTextField();
inputField.setPreferredSize(new Dimension(200, 30));
sendButton = new JButton("Send");
JPanel inputPanel = new JPanel(new BorderLayout());
inputPanel.add(inputField, BorderLayout.CENTER);
inputPanel.add(sendButton, BorderLayout.EAST);
panel.add(inputPanel, BorderLayout.SOUTH);
frame.setContentPane(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
// 连接服务器
try {
socket = new Socket(SERVER_IP, SERVER_PORT);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
// 发送消息按钮的事件处理
sendButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String message = inputField.getText();
if (!message.equals("")) {
out.println(message);
inputField.setText("");
}
}
});
// 接收消息的线程
Thread receiveThread = new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
String message = in.readLine();
if (message == null) {
break;
}
messageArea.append(message + "\n");
// 滚动到最底部
JScrollBar vertical = scrollPane.getVerticalScrollBar();
vertical.setValue(vertical.getMaximum());
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
receiveThread.start();
// 显示界面
frame.setVisible(true);
}
public static void main(String[] args) {
new Client();
}
}
```
这个示例程序中,服务器端和客户端之间的通信使用的是阻塞式的 IO。在实际的应用中,你可能需要使用非阻塞式的 IO 或者 NIO,以提高程序的并发性能和响应速度。同时,这个程序还有很多需要改进的地方,比如异常处理、UI 界面的美化等等。
阅读全文