5 changed files with 412 additions and 60 deletions
@ -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 |
|||
|
|||
) |
|||
@ -0,0 +1,23 @@ |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
|
|||
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; |
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
/*
|
|||
* rpc_client_basic.c |
|||
* 最基础的RPC客户端实现 |
|||
*/ |
|||
|
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
|
|||
#ifdef _WIN32 |
|||
#include <winsock2.h> |
|||
#include <ws2tcpip.h> |
|||
#pragma comment(lib, "ws2_32.lib") |
|||
#define close closesocket |
|||
#else |
|||
#include <unistd.h> |
|||
#include <sys/socket.h> |
|||
#include <netinet/in.h> |
|||
#include <arpa/inet.h> |
|||
#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; |
|||
} |
|||
@ -0,0 +1,103 @@ |
|||
/*
|
|||
* rpc_client_simple.c |
|||
* 简化版RPC客户端实现 |
|||
*/ |
|||
|
|||
#include "rpc_common.h" |
|||
#include "rpc_transport.h" |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
|
|||
#ifdef _WIN32 |
|||
#include <winsock2.h> |
|||
#include <ws2tcpip.h> |
|||
#pragma comment(lib, "ws2_32.lib") |
|||
#else |
|||
#include <unistd.h> |
|||
#include <sys/socket.h> |
|||
#include <netinet/in.h> |
|||
#include <arpa/inet.h> |
|||
#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 <server_host> <server_port>\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; |
|||
} |
|||
Loading…
Reference in new issue