题目
需求:使用tcp协议,编写客户端(有一个专门存放下载文件的目录files)和服务端(有一个专门存放资源的download目录):
- 当客户端给服务端发送 “list”命令时,服务端给客户端响应download文件夹下所有的文件名称,客户端需要把接收到的文件名称打印到控制台
- 当客户端给服务端发送 “服务端download文件夹下的文件名称” 命令时,服务端给客户端响应 download文件夹下对应的文件内容,客户端要把接受到的文件存储到 files 文件夹下
- 服务端和客户端的通信需要在子线程中完成
- 客户端给服务端发送一个命令,服务端给客户端响应一些内容,看做是一个流程,当这个流程完毕后,通信双方的socket都需要关闭,释放资源。
作答
ClientDemo.java
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
public class ClientDemo {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
Socket socket = new Socket("127.0.0.1", 10000);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());
int count = 0;
while (!scanner.nextLine().equals("list")){
System.out.println("命令无效!");
if (++count == 10){
System.exit(0);
}
}
bufferedOutputStream.write("list".getBytes());
socket.shutdownOutput();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
ArrayList<String> list = new ArrayList<>();
String s;
while ((s = bufferedReader.readLine()) != null){
list.add(s);
}
bufferedReader.close();
bufferedOutputStream.close();
io:while (true){
String inputChoices = scanner.next();
boolean flag = false;
for (String s1 : list) {
if (inputChoices.equals(s1)){
BufferedOutputStream bufferedOutputStream1 = new BufferedOutputStream(socket.getOutputStream());
bufferedOutputStream1.write(s1.getBytes());
flag = true;
break io;
}
}
if (!flag){
System.out.println("未找到输入的文件!");
}
}
BufferedInputStream bufferedInputStream = new BufferedInputStream(socket.getInputStream());
BufferedOutputStream bufferedOutputStream1 = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Ensio\\Desktop\\HeiMa\\题库(2)\\题库\\robin\\files\\"));
socket.close();
}
}
ServerDemo.java
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(10000);
Socket accept = serverSocket.accept();
BufferedInputStream bufferedInputStream = new BufferedInputStream(accept.getInputStream());
String str = readClient(bufferedInputStream);
// C:\Users\Ensio\Desktop\HeiMa\题库(2)\题库\robin\download
File file = new File("C:\\Users\\Ensio\\Desktop\\HeiMa\\题库(2)\\题库\\robin\\download");
ArrayList<String> list = new ArrayList<>();
if ("list".equals(str)) {
BufferedWriter bufferedOutputStream = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
writeFile(file, list, bufferedOutputStream);
}
BufferedInputStream bufferedInputStream1 = new BufferedInputStream(accept.getInputStream());
String fileName = readClient(bufferedInputStream1);
String filePath = findFilePath(fileName, file);
BufferedOutputStream bufferedOutputStream2 = new BufferedOutputStream(accept.getOutputStream());
BufferedInputStream bufferedInputStream2 = new BufferedInputStream(new FileInputStream(filePath));
int len;
while ((len = bufferedInputStream2.read()) != -1){
bufferedOutputStream2.write(len);
}
bufferedInputStream.close();
bufferedInputStream1.close();
bufferedInputStream2.close();
bufferedOutputStream2.close();
serverSocket.close();
}
public static String findFilePath(String s, File file){
File[] files = file.listFiles();
String s1 = "";
for (File file1 : files) {
if (file1.isFile()){
if (file1.getName().equals(s)){
s1 += file1;
}
}else {
findFilePath(s,file1);
}
}
return s1;
}
public static String readClient(BufferedInputStream bufferedInputStream) throws IOException {
byte[] bytes = new byte[1024];
int len;
String str = null;
while ((len = bufferedInputStream.read(bytes)) != -1){
str += new String(bytes, 0, len);
}
return str;
}
public static ArrayList<String> filePathToClient(File file, ArrayList<String> list){
File[] files = file.listFiles();
for (File file1 : files) {
if (file1.isDirectory()){
filePathToClient(file1 , list);
}else {
list.add(file1.getName());
}
}
return list;
}
public static void writeFile(File file, ArrayList<String> list, BufferedWriter bufferedOutputStream) throws IOException {
if (file.exists()){
list = filePathToClient(file, list);
for (String s : list) {
bufferedOutputStream.write(s);
bufferedOutputStream.newLine();
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
}else {
bufferedOutputStream.write("空文件夹!");
}
}
}