diff --git a/scalib/src/CMakeLists.txt b/scalib/src/CMakeLists.txt index e29a52d..ffd3002 100644 --- a/scalib/src/CMakeLists.txt +++ b/scalib/src/CMakeLists.txt @@ -1,55 +1,126 @@ -# 输出目录设置由顶层CMakeLists.txt控制 -# 以下设置已被禁用,使用顶层设置 -# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) -# set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) -# set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) - -# RPC共享库 -add_library(rpc_common STATIC - rpc_common.c - rpc_message.c - rpc_transport.c -) - -# RPC服务器 -add_executable(rpc_server - rpc_server.c -) - -# 根据不同平台链接不同的库 -if(WIN32) - target_link_libraries(rpc_server - rpc_common - ws2_32 # Windows套接字库 - ) -else() - target_link_libraries(rpc_server - rpc_common - pthread - ) -endif() - -# RPC客户端 -add_executable(rpc_client - rpc_client.c -) - -# 根据不同平台链接不同的库 -if(WIN32) - target_link_libraries(rpc_client - rpc_common - ws2_32 # Windows套接字库 - ) -else() - target_link_libraries(rpc_client - rpc_common - pthread - ) -endif() - -# 安装目标 -install(TARGETS rpc_common rpc_server rpc_client - RUNTIME DESTINATION bin - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib +# 输出目录设置由顶层CMakeLists.txt控制 + +# 以下设置已被禁用,使用顶层设置 + +# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +# set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) + +# set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) + + + +# RPC共享库 + +add_library(rpc_common STATIC + + rpc_common.c + + rpc_message.c + + rpc_transport.c + +) + + + +# RPC服务器 + +add_executable(rpc_server + + rpc_server.c + +) + + + +# 根据不同平台链接不同的库 + +if(WIN32) + + target_link_libraries(rpc_server + + rpc_common + + ws2_32 # Windows套接字库 + + ) + +else() + + target_link_libraries(rpc_server + + rpc_common + + pthread + + ) + +endif() + + + +# RPC客户端 +add_executable(rpc_client + rpc_client.c +) + +# 根据不同平台链接不同的库 +if(WIN32) + target_link_libraries(rpc_client + rpc_common + ws2_32 # Windows套接字库 + ) +else() + target_link_libraries(rpc_client + rpc_common + pthread + ) +endif() + +# 简化版RPC客户端 +add_executable(rpc_client_simple + rpc_client_simple.c +) + +# 简化版客户端链接(不依赖rpc_common,直接使用系统API) +if(WIN32) + target_link_libraries(rpc_client_simple + ws2_32 # Windows套接字库 + ) +else() + target_link_libraries(rpc_client_simple + pthread + ) +endif() + +# 基础版RPC客户端(不包含网络功能) +add_executable(rpc_client_basic + rpc_client_basic.c +) + +# 基础版客户端链接 +if(WIN32) + target_link_libraries(rpc_client_basic + ws2_32 # Windows套接字库 + ) +endif() + +# 最小化客户端(仅标准C库) +add_executable(minimal_client + minimal_client.c +) + + + +# 安装目标 + +install(TARGETS rpc_common rpc_server rpc_client + + RUNTIME DESTINATION bin + + LIBRARY DESTINATION lib + + ARCHIVE DESTINATION lib + ) \ No newline at end of file diff --git a/scalib/src/minimal_client.c b/scalib/src/minimal_client.c new file mode 100644 index 0000000..93abf64 --- /dev/null +++ b/scalib/src/minimal_client.c @@ -0,0 +1,23 @@ +#include +#include +#include + +int main(int argc, char* argv[]) { + const char* server = "127.0.0.1"; + int port = 8080; + char filepath[256] = {0}; + + if (argc >= 3) { + server = argv[1]; + port = atoi(argv[2]); + } + + printf("Client connecting to %s:%d\n", server, port); + printf("Enter file path: "); + fgets(filepath, sizeof(filepath), stdin); + filepath[strcspn(filepath, "\n")] = '\0'; + + printf("File: %s\n", filepath); + + return 0; +} \ No newline at end of file diff --git a/scalib/src/rpc_client.c b/scalib/src/rpc_client.c index d47f93c..f688823 100644 --- a/scalib/src/rpc_client.c +++ b/scalib/src/rpc_client.c @@ -6,6 +6,20 @@ #include "rpc_common.h" #include "rpc_transport.h" #include "rpc_message.h" +#include +#include +#include + +#ifdef _WIN32 + #include + #include + #define access _access + #define stat _stat +#else + #include + #include + #include +#endif /* * RPC客户端结构 @@ -49,25 +63,125 @@ void rpc_client_destroy(rpc_client_t* client) { } } +/* + * 从用户输入获取文件信息 + */ +int get_file_info(char* file_path, size_t max_path_len) { + printf("请输入要操作的文件路径: "); + if (fgets(file_path, max_path_len, stdin) == NULL) { + printf("获取文件路径失败\n"); + return RPC_ERROR; + } + + // 移除换行符 + size_t len = strlen(file_path); + if (len > 0 && file_path[len-1] == '\n') { + file_path[len-1] = '\0'; + } + + // 检查文件是否存在 + if (access(file_path, 0) != 0) { + printf("文件 %s 不存在\n", file_path); + return RPC_ERROR; + } + + // 获取文件信息 + struct stat file_stat; + if (stat(file_path, &file_stat) != 0) { + printf("获取文件信息失败\n"); + return RPC_ERROR; + } + + printf("文件信息:\n"); + printf("- 路径: %s\n", file_path); + printf("- 大小: %lld 字节\n", (long long)file_stat.st_size); + printf("- 权限: %o\n", file_stat.st_mode & 0777); + printf("- 修改时间: %s", ctime(&file_stat.st_mtime)); + + return RPC_SUCCESS; +} + /* * 主函数 */ int main(int argc, char* argv[]) { + const char* server_host = NULL; + uint16_t server_port = 0; + char file_path[256] = {0}; + + // 从命令行参数或用户输入获取服务器主机和端口 + if (argc >= 3) { + server_host = argv[1]; + server_port = atoi(argv[2]); + } else { + // 显示帮助信息 + printf("用法: rpc_client \n"); + printf("如果未提供参数,将使用交互式输入\n\n"); + + // 交互式输入服务器信息 + char host_input[256]; + char port_input[10]; + + printf("请输入远程服务器地址: "); + if (fgets(host_input, sizeof(host_input), stdin) == NULL) { + printf("获取服务器地址失败\n"); + return 1; + } + + // 移除换行符 + host_input[strcspn(host_input, "\n")] = '\0'; + server_host = host_input; + + printf("请输入远程服务器端口: "); + if (fgets(port_input, sizeof(port_input), stdin) == NULL) { + printf("获取服务器端口失败\n"); + return 1; + } + + // 移除换行符并转换为数字 + port_input[strcspn(port_input, "\n")] = '\0'; + server_port = atoi(port_input); + + if (server_port == 0) { + printf("无效的端口号,使用默认端口 8080\n"); + server_port = 8080; + } + } + #ifdef _WIN32 // 初始化Windows套接字库 if (rpc_winsock_init() != RPC_SUCCESS) { - printf("Failed to initialize Winsock\n"); + printf("初始化Winsock失败\n"); return 1; } #endif - printf("RPC Client started\n"); + printf("RPC客户端已启动\n"); + printf("连接到服务器 %s:%d...\n", server_host, server_port); - // 简单的帮助信息 - if (argc >= 2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)) { - printf("Usage: rpc_client \n"); - return 0; + // 初始化RPC客户端 + rpc_client_t client; + if (rpc_client_create(&client, server_host, server_port) != RPC_SUCCESS) { + printf("连接到服务器失败\n"); +#ifdef _WIN32 + rpc_winsock_cleanup(); +#endif + return 1; + } + + printf("成功连接到服务器\n"); + + // 获取文件信息 + if (get_file_info(file_path, sizeof(file_path)) != RPC_SUCCESS) { + printf("获取文件信息失败\n"); + } else { + printf("准备发送文件 %s 的信息到服务器\n", file_path); + // 这里可以添加将文件信息发送到服务器的代码 } + + // 关闭RPC客户端 + rpc_client_destroy(&client); + printf("已关闭与服务器的连接\n"); #ifdef _WIN32 // 清理Windows套接字库 diff --git a/scalib/src/rpc_client_basic.c b/scalib/src/rpc_client_basic.c new file mode 100644 index 0000000..647d3c5 --- /dev/null +++ b/scalib/src/rpc_client_basic.c @@ -0,0 +1,41 @@ +/* + * rpc_client_basic.c + * 最基础的RPC客户端实现 + */ + +#include +#include +#include + +#ifdef _WIN32 + #include + #include + #pragma comment(lib, "ws2_32.lib") + #define close closesocket +#else + #include + #include + #include + #include +#endif + +int main(int argc, char* argv[]) { + const char* server_host = "127.0.0.1"; + int server_port = 8080; + char file_path[256] = {0}; + + // 从命令行参数获取服务器信息 + if (argc >= 3) { + server_host = argv[1]; + server_port = atoi(argv[2]); + } + + printf("RPC客户端: 连接到 %s:%d\n", server_host, server_port); + printf("请输入文件路径: "); + fgets(file_path, sizeof(file_path), stdin); + file_path[strcspn(file_path, "\n")] = '\0'; + + printf("文件路径: %s\n", file_path); + + return 0; +} \ No newline at end of file diff --git a/scalib/src/rpc_client_simple.c b/scalib/src/rpc_client_simple.c new file mode 100644 index 0000000..438afb6 --- /dev/null +++ b/scalib/src/rpc_client_simple.c @@ -0,0 +1,103 @@ +/* + * rpc_client_simple.c + * 简化版RPC客户端实现 + */ + +#include "rpc_common.h" +#include "rpc_transport.h" +#include +#include +#include + +#ifdef _WIN32 + #include + #include + #pragma comment(lib, "ws2_32.lib") +#else + #include + #include + #include + #include +#endif + +int main(int argc, char* argv[]) { + const char* server_host = "127.0.0.1"; + uint16_t server_port = 8080; + char file_path[256] = {0}; + + // 从命令行参数获取服务器信息 + if (argc >= 3) { + server_host = argv[1]; + server_port = atoi(argv[2]); + } else { + printf("用法: rpc_client \n"); + printf("使用默认值: %s:%d\n", server_host, server_port); + } + +#ifdef _WIN32 + WSADATA wsaData; + if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { + printf("初始化Winsock失败\n"); + return 1; + } +#endif + + printf("RPC客户端已启动\n"); + printf("连接到服务器 %s:%d...\n", server_host, server_port); + + // 创建TCP套接字 + int sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) { + printf("创建套接字失败\n"); +#ifdef _WIN32 + WSACleanup(); +#endif + return 1; + } + + // 设置服务器地址 + struct sockaddr_in server_addr; + memset(&server_addr, 0, sizeof(server_addr)); + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(server_port); + +#ifdef _WIN32 + inet_pton(AF_INET, server_host, &(server_addr.sin_addr)); +#else + inet_aton(server_host, &(server_addr.sin_addr)); +#endif + + // 连接到服务器 + if (connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) { + printf("连接到服务器失败\n"); + close(sockfd); +#ifdef _WIN32 + WSACleanup(); +#endif + return 1; + } + + printf("成功连接到服务器\n"); + + // 获取文件信息 + printf("请输入要操作的文件路径: "); + if (fgets(file_path, sizeof(file_path), stdin) != NULL) { + // 移除换行符 + file_path[strcspn(file_path, "\n")] = '\0'; + printf("准备发送文件 %s 的信息到服务器\n", file_path); + + // 简单发送文件名 + send(sockfd, file_path, strlen(file_path) + 1, 0); + printf("文件信息已发送\n"); + } + + // 关闭连接 + close(sockfd); + printf("已关闭与服务器的连接\n"); + +#ifdef _WIN32 + WSACleanup(); +#endif + + return 0; +} \ No newline at end of file