5 changed files with 412 additions and 60 deletions
@ -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