This commit is contained in:
tomyee 2024-12-18 00:21:22 +08:00
parent fa6f029eea
commit 23987b5975
13 changed files with 99 additions and 56 deletions

View File

@ -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

51
.gitignore vendored Normal file
View File

@ -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

View File

@ -7,6 +7,7 @@
"in.h": "c",
"rand.h": "c",
"inet.h": "c",
"unistd.h": "c"
"unistd.h": "c",
"stdio.h": "c"
}
}

View File

@ -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、客户端根据提示输入命令及内容

BIN
client

Binary file not shown.

View File

@ -5,6 +5,7 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#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;
}

View File

@ -0,0 +1,2 @@
123
EOFDOWNLOAD123_decrypted.txt

View File

View File

1
newfile/123.txt Normal file
View File

@ -0,0 +1 @@
123

1
newfile/init.sh Normal file
View File

@ -0,0 +1 @@
123

BIN
server

Binary file not shown.

View File

@ -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)