// TestEcho-SSL-Console Client-Native (重构版) // 纯OpenSSL+Winsock实现的SSL客户端(现代项目结构) #include "pch.h" #include "CertificateConfig.h" #include "SSLClientConnection.h" using namespace SSLClient; // 服务器配置 static const char* DEFAULT_ADDRESS = "127.0.0.1"; static const int DEFAULT_PORT = 5555; /// /// 打印命令菜单 /// void PrintMenu() { std::cout << "\n命令菜单:" << std::endl; std::cout << " 1 - 发送 \"hello\"" << std::endl; std::cout << " q - 退出程序" << std::endl; std::cout << "请输入命令: " << std::flush; } int SSL_Client() { std::cout << "========================================" << std::endl; std::cout << " SSL Client Native (纯OpenSSL+Winsock)" << std::endl; std::cout << " 现代化项目结构版本" << std::endl; std::cout << "========================================" << std::endl; std::cout << std::endl; // 创建SSL客户端连接对象 SSLClientConnection client; // 初始化SSL环境 if (!client.Initialize( CertificateConfig::GetClientCertificate(), CertificateConfig::GetClientPrivateKey(), CertificateConfig::GetCACertificate(), CertificateConfig::GetKeyPassword())) { std::cout << "按任意键退出..." << std::endl; _getch(); return 1; } // 连接到服务器 if (!client.Connect(DEFAULT_ADDRESS, DEFAULT_PORT)) { std::cout << "连接失败,按任意键退出..." << std::endl; _getch(); return 1; } // 等待连接建立 std::cout << "[客户端] 已连接到服务器,可以开始发送消息" << std::endl; // 主循环 PrintMenu(); bool running = true; char receiveBuffer[1024]; while (running) { // 检查是否有数据可接收 int received = client.Receive(receiveBuffer, sizeof(receiveBuffer)); if (received < 0) { // 接收错误,可能连接已断开 std::cerr << "[错误] 接收数据失败,连接可能已断开" << std::endl; break; } else { } // 检查键盘输入 if (_kbhit()) { char ch = _getch(); std::cout << ch << std::endl; if (ch == '1') { // 发送 "hello" if (client.Send("hello")) { Sleep(100); // 等待服务器响应 client.Receive(receiveBuffer, sizeof(receiveBuffer)); // 接收响应 } PrintMenu(); } else if (ch == 'q' || ch == 'Q') { running = false; } else { std::cout << "无效命令,请重新输入。" << std::endl; PrintMenu(); } } Sleep(100); } // 断开连接 client.Disconnect(); } /// /// 主函数 /// int main() { // 设置控制台UTF-8编码 SetConsoleOutputCP(CP_UTF8); std::locale::global(std::locale("")); // 设置全局区域设置 // 运行SSL客户端 SSL_Client(); std::cout << "按任意键退出..." << std::endl; _getch(); return 0; }