Files
--hp-socket-TCP--ssl--/使用指南.md
2026-01-23 08:39:07 +08:00

218 lines
5.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# TestEcho-SSL-Console 快速使用指南
## 编译步骤
### 方法1使用Visual Studio 2022
1. 使用VS2022打开解决方案文件
```
e:\开发\C_C++\hp-socket-6.0.4\hp-socket-6.0.7-src\Windows\Demo\TestEcho-SSL-Console\TestEcho-SSL-Console.sln
```
2. 选择配置:
- 配置Debug 或 Release
- 平台x64推荐或 x86
3. 点击菜单:**生成 -> 生成解决方案** (或按 Ctrl+Shift+B
4. 编译完成后,可执行文件位于:
- Debug x64: `e:\开发\C_C++\hp-socket-6.0.4\hp-socket-6.0.7-src\Windows\Demo\Debug\x64\`
- Release x64: `e:\开发\C_C++\hp-socket-6.0.4\hp-socket-6.0.7-src\Windows\Demo\Release\x64\`
### 方法2使用命令行编译需要配置VS环境
打开 **x64 Native Tools Command Prompt for VS 2022**
```cmd
cd /d "e:\开发\C_C++\hp-socket-6.0.4\hp-socket-6.0.7-src\Windows\Demo\TestEcho-SSL-Console"
msbuild TestEcho-SSL-Console.sln /p:Configuration=Release /p:Platform=x64
```
## 运行步骤
### 第一步:启动服务器
打开命令提示符窗口:
```cmd
cd /d "e:\开发\C_C++\hp-socket-6.0.4\hp-socket-6.0.7-src\Windows\Demo\Debug\x64"
TestEcho-SSL-Console-Server.exe
```
你将看到:
```
========================================
HP-Socket SSL Server 控制台演示程序
========================================
[服务器] 正在初始化SSL环境...
[服务器] SSL环境初始化成功
[服务器] 正在启动服务器...
[服务器] 准备监听: 0.0.0.0:5555
[服务器] 服务器启动成功,监听端口: 5555
[服务器] 按 'Q' 键停止服务器...
```
### 第二步:启动客户端
打开**另一个**命令提示符窗口:
```cmd
cd /d "e:\开发\C_C++\hp-socket-6.0.4\hp-socket-6.0.7-src\Windows\Demo\Debug\x64"
TestEcho-SSL-Console-Client.exe
```
你将看到:
```
========================================
HP-Socket SSL Client 控制台演示程序
========================================
[客户端] 正在初始化SSL环境...
[客户端] SSL环境初始化成功
[客户端] 正在连接服务器 127.0.0.1:5555 ...
[客户端] 连接成功: 本地地址=127.0.0.1:xxxxx
[客户端] SSL握手完成
[客户端] 已连接到服务器,可以开始发送消息
----------------------------------------
命令菜单:
1 - 发送 'hello' 到服务器
Q - 断开连接并退出
----------------------------------------
请输入命令:
```
### 第三步:测试通信
在客户端窗口中:
1. **按键盘上的 `1`** 发送消息到服务器
客户端显示:
```
1
[客户端] 发送消息: "hello"
[客户端] 收到服务器响应: "hello!"
```
服务器显示:
```
[服务器] 客户端连接: ID=1, 地址=127.0.0.1:xxxxx
[服务器] SSL握手完成: ID=1
[服务器] 接收到消息: ID=1, 内容="hello from client"
[服务器] 发送响应: "hello!"
```
2. **按 `Q` 键退出客户端**
3. 在服务器窗口中**按 `Q` 键停止服务器**
## 测试场景
### 场景1基本的消息收发
1. 启动服务器
2. 启动客户端
3. 在客户端按 `1` 发送消息
4. 观察服务器收到 "hello from client" 并返回 "hello!"
5. 观察客户端收到服务器的 "hello!" 响应
### 场景2多次发送
1. 启动服务器和客户端
2. 在客户端多次按 `1` 键
3. 每次都会发送消息并收到响应
### 场景3客户端重连
1. 启动服务器
2. 启动客户端并发送消息
3. 关闭客户端(按 Q
4. 再次启动客户端
5. 继续发送消息
## 常见问题
### Q1: 编译时找不到OpenSSL库
**问题**:编译错误提示找不到 `libssl.lib` 或 `libcrypto.lib`
**解决方案**
1. 检查路径 `e:\开发\C_C++\hp-socket-6.0.4\hp-socket-6.0.7-src\Windows\Dependent\openssl\v143\x64\lib\` 是否存在
2. 如果不存在 `v143` 目录可能需要调整项目文件中的OpenSSL路径
3. 检查是否有其他版本的工具集目录(如 v142, v141等
### Q2: 运行时找不到DLL
**问题**:运行时提示找不到 `libssl-3-x64.dll``libcrypto-3-x64.dll`
**解决方案**
1. 将DLL文件从 `Windows\Dependent\openssl\v143\x64\bin\` 复制到程序所在目录
2. 或将该bin目录添加到系统PATH环境变量
### Q3: 服务器启动失败,端口被占用
**问题**服务器启动失败提示端口5555已被占用
**解决方案**
```cmd
# 查看端口占用情况
netstat -ano | findstr 5555
# 如果需要,结束占用端口的进程
taskkill /F /PID <进程ID>
```
### Q4: 客户端无法连接服务器
**问题**:客户端连接超时
**解决方案**
1. 确认服务器已经启动
2. 检查防火墙是否阻止了连接
3. 确认服务器监听的是 0.0.0.0:5555
4. 如果是远程连接,修改客户端代码中的 `DEFAULT_ADDRESS`
## 代码自定义
### 修改监听端口
编辑 `Server/main.cpp`
```cpp
static const USHORT PORT = 5555; // 修改为你想要的端口
```
编辑 `Client/main.cpp`
```cpp
static const USHORT DEFAULT_PORT = 5555; // 修改为与服务器相同的端口
```
### 修改服务器地址
编辑 `Client/main.cpp`
```cpp
static const LPCTSTR DEFAULT_ADDRESS = _T("127.0.0.1"); // 修改为服务器IP
```
### 自定义消息内容
编辑 `Client/main.cpp`,在 `if (ch == '1')` 代码块中:
```cpp
std::string message = "hello"; // 修改发送的内容
```
编辑 `Server/main.cpp`,在 `OnReceive` 方法中:
```cpp
std::string response = "hello!"; // 修改响应的内容
```
## 技术支持
如有问题,请参考:
- HP-Socket官方文档
- README.md 完整说明
- Windows\Demo\TestEcho-SSL\ 原始示例项目
祝使用愉快!