diff --git a/.bash_history b/.bash_history deleted file mode 100644 index 1c68039..0000000 --- a/.bash_history +++ /dev/null @@ -1,47 +0,0 @@ -ls -。./linux -./linux -exit -ls -./init.sh -exit -gcc -o linux linux.c -lpthread -lssl -lcrypto -gcc -o linux linux.c -lpthread -lssl -lcrypto -gcc -o linux linux.c -lpthread -lssl -lcrypto -./linux -openssl ecparam -name SM2 -genkey -noout -out sm2_key.pem -openssl ec -in sm2_key.pem -pubout -out sm2_pub.pem -ll -ls -cat sm2_key.pem -ls -exit -gcc client.c -o client -lssl -lcrypto -openssl version -openssl-devel version -yum install -y openssl-devel -find /usr/include/openssl -name sm4.h -which openssl -find /usr/bin/openssl -name sm4.h -find /usr/bin/openssl/ -name sm4.h -cd /usr/bin/openssl -wget https://www.openssl.org/source/openssl-3.0.12.tar.gz -yum install wget -wget https://www.openssl.org/source/openssl-3.0.12.tar.gz -find / -name sm4.h -cd / -ls -find / -name sm4.h -cd /usr/include/ -ll -ls -ls -ls -l -exit -ll -alias ll 'ls -l' -alias 'll' 'ls -l' -alias ll 'ls -l' -alias 'ls -l' ll -gcc -o server server.c -lpthread -L/usr/local/lib -I/usr/local/include -lgmssl -lpthread -lz -exit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..78966f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,51 @@ + +# generated file +/build* +/demos/*.pem +/demos/.private +/demos/scripts/*.pem +/demos/scripts/*.der +/demos/scripts/*.txt +/demos/scripts/*.bin +/demos/scripts/*.sig + +/examples/* + +# Object files +*.o +*.obj + +# All kinds of executables +*.so +*.so.* +*.dylib +*.dylib.* +*.dll +*.dll.* +*.exe +*.pyc +*.exp +*.lib +*.pdb +*.ilk +*.def +*.rc +*.res + +# macOS +.DS_Store +*.tar.gz + +# editor artefacts +.vscode/* +*.swp +.#* +\#*# +*~ + +/docs/* + +/.vs/* +/out/* + +*.plist diff --git a/.vscode/settings.json b/.vscode/settings.json index 1042273..fe0b90a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,6 +7,7 @@ "in.h": "c", "rand.h": "c", "inet.h": "c", - "unistd.h": "c" + "unistd.h": "c", + "stdio.h": "c" } } \ No newline at end of file diff --git a/README.md b/README.md index 19f93be..79898e4 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,20 @@ > macOS + Docker 部署 openeuler/openeuler(详细见 Dockefile 以及 docker-compose.yml) 技术: + 1. 客户端根据提示创建并上传一个文件到服务端。 + 2. 服务端对文件进行SM4加密后压缩并存储。 + 3. 客户端请求下载文件时,服务端解压解密文件,将内容返回客户端。 + 4. 使用线程和信号量限制客户端的并发连接数。 + 5. 通过进程间通信记录操作日志(使用管道)。 + 6. 客户端和服务端通过 socket 通信。 + 7. 文件操作会创建时间戳的目录管理文件。 ~~~bash gcc -o server server.c -lpthread -L/usr/local/lib -I/usr/local/include -lgmssl -lz gcc -o client client.c ~~~ + +启动测试: +1、启动服务端:./server +2、启动客户端:./client +3、客户端根据提示输入命令及内容 \ No newline at end of file diff --git a/client b/client index 030db23..9a3c20d 100755 Binary files a/client and b/client differ diff --git a/client.c b/client.c index f0a6545..cf50af2 100644 --- a/client.c +++ b/client.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #define PORT 8001 @@ -14,18 +15,37 @@ void upload_file(int client_socket) { char file_name[BUFFER_SIZE]; char file_content[BUFFER_SIZE]; - + mkdir("./newfile", 0777); // 用户输入文件名 - printf("输入要创建的文件名(不带路径): "); + printf("输入要创建的文件名(不带路径,需携带后缀)例如:abc.txt: "); scanf("%s", file_name); + // 提取文件名部分(去掉后缀) + char base_name[BUFFER_SIZE]; + const char *dot = strrchr(file_name, '.'); + if (dot && dot != file_name) + { + strncpy(base_name, file_name, dot - file_name); + base_name[dot - file_name] = '\0'; + } + else + { + // 如果没有后缀,直接使用原文件名 + strcpy(base_name, file_name); + } + printf("文件名部分是: %s\n", base_name); + // 用户输入文件内容 printf("输入文件内容(输入完毕后按回车结束):\n"); getchar(); // 清除上一次输入的换行符 fgets(file_content, BUFFER_SIZE, stdin); - // 以file_name为文件名创建文件并将输入内容写入文件 - FILE *plain_file = fopen(file_name, "w"); + char dir_name[256]; + snprintf(dir_name, 256, "./newfile/%s", file_name); + printf("文件路径是: %s\n", dir_name); + + // 根据dir_name目录,file_name为文件名创建文件并将输入内容写入文件 + FILE *plain_file = fopen(dir_name, "w"); if (!plain_file) { perror("文件创建失败"); @@ -35,15 +55,17 @@ void upload_file(int client_socket) fclose(plain_file); printf("文件创建成功,开始上传。\n"); - // 发送文件名到服务器 - send(client_socket, file_name, strlen(file_name), 0); + // 发送文件名(不含后缀)到服务器 + send(client_socket, base_name, strlen(base_name), 0); // 等待服务器确认收到文件名 char ack[BUFFER_SIZE] = {0}; read(client_socket, ack, BUFFER_SIZE); if (strcmp(ack, "ACK") == 0) { printf("服务端确认文件名。\n"); - }else{ + } + else + { printf("服务端未确认文件名。\n"); return; } diff --git a/files/20241217111950/123.txt b/files/20241217111950/123.txt new file mode 100644 index 0000000..c36f8ce --- /dev/null +++ b/files/20241217111950/123.txt @@ -0,0 +1,2 @@ +123 +EOFDOWNLOAD123_decrypted.txt \ No newline at end of file diff --git a/files/20241218000302/123.txt b/files/20241218000302/123.txt new file mode 100644 index 0000000..e69de29 diff --git a/files/20241218001945/DOWNLOAD123.txt.txt b/files/20241218001945/DOWNLOAD123.txt.txt new file mode 100644 index 0000000..e69de29 diff --git a/newfile/123.txt b/newfile/123.txt new file mode 100644 index 0000000..190a180 --- /dev/null +++ b/newfile/123.txt @@ -0,0 +1 @@ +123 diff --git a/newfile/init.sh b/newfile/init.sh new file mode 100644 index 0000000..190a180 --- /dev/null +++ b/newfile/init.sh @@ -0,0 +1 @@ +123 diff --git a/server b/server index 34a91e8..6a2f32c 100755 Binary files a/server and b/server differ diff --git a/server.c b/server.c index ad5688d..b97640a 100644 --- a/server.c +++ b/server.c @@ -411,7 +411,7 @@ int main() while (1) { dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); - printf("客户端进入,当前资源被占用\n"); + // printf("客户端进入,当前资源被占用\n"); int client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len); if (client_socket == -1)