改写socket服务器端程序(即实验1中参考example11-5中server.java的编程结果),设计编写出一个TCP服务器端程序:要求使用线程池处理客户端的连接请求。相应需修改socket客户端程序:从本地一个txt文件(注意:客户端的此txt文件称为信源txt文件,该txt测试数据文件可自由选择或创建,只要里面包含至少10行字符串即可),逐行读取出来,每一行作为一句话,发送给服务器,服务器每收到一个客户端的一句话,就将其保存在服务器本地的一个专用于存放与该客户端通信内容的txt文件中(注意:称为通信记录txt文件,来自不同的客户端的信息需保存在不同的txt文件中,需多少个txt文件应在程序中自动根据实际连接的客户端数量自动创建),服务器还会计算每一个通信记录txt文件的安全算法摘要,将安全算法摘要和该通信记录txt文件名一起写入到服务器中一个名为SafeAbstract.txt的文件中。
时间: 2023-05-27 08:08:02 浏览: 98
TCP服务器端程序:
```java
import java.io.*;
import java.net.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Server {
private static final int PORT = 8888;
private static ExecutorService executorService = Executors.newCachedThreadPool();
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("Server started.");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Client connected: " + socket.getInetAddress().getHostAddress());
executorService.execute(new ClientHandler(socket));
}
} catch (IOException e) {
e.printStackTrace();
}
}
static class ClientHandler implements Runnable {
private Socket socket;
public ClientHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
String fileName = socket.getInetAddress().getHostAddress() + ".txt";
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName, true)));
while ((line = in.readLine()) != null) {
out.println(line);
out.flush();
}
out.close();
in.close();
socket.close();
writeSafeAbstract(fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeSafeAbstract(String fileName) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
FileInputStream fis = new FileInputStream(fileName);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) != -1) {
md5.update(buffer, 0, length);
}
fis.close();
byte[] digest = md5.digest();
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b));
}
String safeAbstract = sb.toString();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("SafeAbstract.txt", true)));
out.println(fileName + " " + safeAbstract);
out.flush();
out.close();
} catch (NoSuchAlgorithmException | IOException e) {
e.printStackTrace();
}
}
}
}
```
TCP客户端程序:
```java
import java.io.*;
import java.net.*;
public class Client {
private static final String FILENAME = "source.txt";
private static final String SERVER_IP = "127.0.0.1";
private static final int SERVER_PORT = 8888;
public static void main(String[] args) {
try {
BufferedReader in = new BufferedReader(new FileReader(FILENAME));
Socket socket = new Socket(SERVER_IP, SERVER_PORT);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
String line;
while ((line = in.readLine()) != null) {
out.println(line);
out.flush();
}
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文