基于Java语言的网络聊天室程序设计,给出代码完成这些要求: 1、使用TCP或UDP协议设计一个客户端程序和一个服务器程序,接收和发送聊天室程序的信息。 2、设计两个界面程序,完成聊天室程序用户发送信息输入和用户接收信息的现实。 3、设计两个文件处理程序,完成聊天双方信息的保存和记录。
时间: 2024-03-16 09:46:50 浏览: 219
很高兴回答您的问题。基于Java语言的网络聊天室程序设计,需要完成以下要求:
1.设计一个服务器程序和一个客户端程序,使用TCP或UDP协议进行通信。
服务器程序:
```java
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
ServerSocket server = null;
Socket socket = null;
BufferedReader in = null;
PrintWriter out = null;
try{
server = new ServerSocket(8888);
socket = server.accept();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
String line = null;
while((line = in.readLine()) != null){
System.out.println("Received message: " + line);
out.println("Server received message: " + line);
out.flush();
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
in.close();
out.close();
socket.close();
server.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
```
客户端程序:
```java
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
Socket socket = null;
BufferedReader in = null;
PrintWriter out = null;
try{
socket = new Socket("127.0.0.1", 8888);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = reader.readLine()) != null){
out.println(line);
out.flush();
System.out.println(in.readLine());
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
in.close();
out.close();
socket.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
```
2.设计两个界面程序,一个用于用户输入信息,一个用于显示接收到的信息。
用户输入信息界面:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ClientGUI extends JFrame {
private JTextField textField;
private JTextArea textArea;
private BufferedReader in;
private PrintWriter out;
private static String name;
public ClientGUI() {
JLabel label = new JLabel("Enter your name: ");
textField = new JTextField(20);
add(label, BorderLayout.WEST);
add(textField, BorderLayout.CENTER);
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
name = textField.getText();
dispose();
initChat();
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private void initChat() {
JFrame frame = new JFrame(name + "'s Chat Room");
JPanel panel = new JPanel(new BorderLayout());
textArea = new JTextArea();
textArea.setEditable(false);
JTextField textField = new JTextField();
panel.add(textArea, BorderLayout.CENTER);
panel.add(textField, BorderLayout.SOUTH);
frame.add(panel);
frame.setSize(500, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
try{
Socket socket = new Socket("127.0.0.1", 8888);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
out.println(name);
out.flush();
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String message = textField.getText();
out.println(name + ": " + message);
out.flush();
textField.setText("");
}
});
while(true){
String message = in.readLine();
textArea.append(message + "\n");
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
new ClientGUI();
}
}
```
用户显示信息界面:
```java
import javax.swing.*;
import java.io.*;
import java.net.*;
public class ServerGUI extends JFrame {
private JTextArea textArea;
public ServerGUI() {
textArea = new JTextArea();
add(textArea);
setSize(500, 300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
try{
ServerSocket server = new ServerSocket(8888);
while(true){
Socket socket = server.accept();
new ServerThread(socket).start();
}
}catch(Exception e){
e.printStackTrace();
}
}
private class ServerThread extends Thread{
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ServerThread(Socket socket){
this.socket = socket;
try{
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
}catch(Exception e){
e.printStackTrace();
}
}
public void run(){
try{
String name = in.readLine();
while(true){
String message = in.readLine();
textArea.append(name + ": " + message + "\n");
out.println("Server received message: " + message);
out.flush();
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
in.close();
out.close();
socket.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new ServerGUI();
}
}
```
3.设计两个文件处理程序,完成聊天双方信息的保存和记录。
保存聊天信息程序:
```java
import java.io.*;
import java.net.*;
public class SaveChat {
public static void main(String[] args) {
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String name = null;
PrintWriter out = null;
while(true){
System.out.println("Enter your name: ");
name = in.readLine();
if(name.trim().length() > 0){
break;
}
}
Socket socket = new Socket("127.0.0.1", 8888);
out = new PrintWriter(new FileWriter(name + ".txt"), true);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
while((line = reader.readLine()) != null){
out.println(line);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
```
记录聊天信息程序:
```java
import java.io.*;
public class RecordChat {
public static void main(String[] args) {
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your name: ");
String name = in.readLine();
BufferedReader reader = new BufferedReader(new FileReader(name + ".txt"));
String line = null;
while((line = reader.readLine()) != null){
System.out.println(line);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
```
以上是基于Java语言的网络聊天室程序设计的所有代码,希望对您有所帮助。
阅读全文