使用java编写网络客户/服务器程序,实现如下功能: (1)设计服务器程序,运行时等待客户端连接; (2)客户端设计如下界面,并在启动时与服务器建立连接;界面有一个输入消息,用户输入年份以后,点击发送按钮,下面的文本框中将会显示服务器返回的消息,即将小写转换为大写(3) 客户端发送消息, 服务器收到消息后, 将所有小写字符转换为大写字 符,发回给客户端; (4)客户端收到服务器发回的消息后,在界面上显示。写出完整代码给我
时间: 2024-03-22 10:37:57 浏览: 78
这里提供一个简单的Java网络客户端/服务器程序,实现将客户端输入的年份转换为大写字母并返回给客户端。
服务器端代码:
```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;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
Socket clientSocket = null;
try {
serverSocket = new ServerSocket(8080); // 监听8080端口
System.out.println("Server started, listening on port 8080...");
} catch (IOException e) {
System.err.println("Could not listen on port: 8080.");
System.exit(1);
}
while (true) {
try {
clientSocket = serverSocket.accept(); // 等待客户端连接
System.out.println("Client connected: " + clientSocket.getInetAddress());
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
// 处理客户端请求
new Thread(new ServerThread(clientSocket)).start();
}
}
}
class ServerThread implements Runnable {
private Socket clientSocket;
public ServerThread(Socket clientSocket) {
this.clientSocket = clientSocket;
}
@Override
public void run() {
try {
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Received from client: " + inputLine);
// 转换为大写字母并返回给客户端
String outputLine = inputLine.toUpperCase();
out.println(outputLine);
System.out.println("Sent to client: " + outputLine);
}
out.close();
in.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
客户端代码:
```java
import javax.swing.*;
import java.awt.*;
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;
public class Client extends JFrame {
private JTextField inputField;
private JTextArea outputArea;
private PrintWriter out;
private BufferedReader in;
public Client() {
super("Client");
// UI界面
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
add(panel, BorderLayout.NORTH);
JLabel label = new JLabel("Enter a year:");
panel.add(label, BorderLayout.WEST);
inputField = new JTextField();
panel.add(inputField, BorderLayout.CENTER);
JButton sendButton = new JButton("Send");
panel.add(sendButton, BorderLayout.EAST);
outputArea = new JTextArea();
outputArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(outputArea);
add(scrollPane, BorderLayout.CENTER);
// 与服务器建立连接
try {
Socket socket = new Socket("localhost", 8080); // 连接服务器
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
// 发送消息
sendButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String year = inputField.getText();
out.println(year);
try {
String response = in.readLine();
outputArea.append(response + "\n");
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
setVisible(true);
}
public static void main(String[] args) {
new Client();
}
}
```
在运行Server.java后,再运行Client.java即可看到UI界面。在输入框中输入年份,点击Send按钮,下面的文本框中将会显示服务器返回的消息(将小写字母转换为大写字母)。
阅读全文