Files
--hp-socket-TCP--ssl--/README.md
2026-01-23 08:39:07 +08:00

193 lines
5.2 KiB
Markdown
Raw Permalink 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 演示程序
这是一个基于HP-Socket的SSL服务器和客户端控制台演示程序。
## 项目结构
```
TestEcho-SSL-Console/
├── Server/ # SSL服务器控制台程序
│ ├── main.cpp
│ └── Server.vcxproj
├── Client/ # SSL客户端控制台程序
│ ├── main.cpp
│ └── Client.vcxproj
├── TestEcho-SSL-Console.sln # VS2022解决方案文件
└── README.md # 本文件
```
## 功能特性
### 服务器端 (Server)
- 监听端口5555
- 绑定地址0.0.0.0(所有网络接口)
- SSL/TLS加密通信
- 接收客户端消息并在控制台显示
- 自动向客户端发送响应消息
- 按 'Q' 键停止服务器
### 客户端 (Client)
- 连接到127.0.0.1:5555
- SSL/TLS加密通信
- 交互式命令界面
- 命令:
- **1** - 发送 "hello" 消息到服务器
- **Q** - 断开连接并退出
## 编译和运行
### 前提条件
- Visual Studio 2022
- HP-Socket源码库
- OpenSSL库已包含在 `Windows/Dependent/openssl/` 目录中)
### 编译步骤
1. 打开 Visual Studio 2022
2. 打开解决方案文件:`TestEcho-SSL-Console.sln`
3. 选择配置Debug 或 Release和平台x86 或 x64
4. 生成解决方案Ctrl+Shift+B
编译成功后,可执行文件将生成在:
- `Windows/Demo/Debug/x64/``Windows/Demo/Release/x64/`64位
- `Windows/Demo/Debug/x86/``Windows/Demo/Release/x86/`32位
### 运行步骤
#### 方式1使用命令行
1. **启动服务器**
```
cd Windows\Demo\Debug\x64
TestEcho-SSL-Console-Server.exe
```
2. **启动客户端**(在另一个命令行窗口)
```
cd Windows\Demo\Debug\x64
TestEcho-SSL-Console-Client.exe
```
#### 方式2从Visual Studio运行
1. 右键点击 Server 项目 -> 设为启动项目
2. 按 F5 或 Ctrl+F5 运行服务器
3. 右键点击 Client 项目 -> 设为启动项目
4. 按 F5 或 Ctrl+F5 运行客户端
## 使用示例
### 服务器端输出示例:
```
========================================
HP-Socket SSL Server 控制台演示程序
========================================
[服务器] 正在初始化SSL环境...
[服务器] SSL环境初始化成功
[服务器] 正在启动服务器...
[服务器] 准备监听: 0.0.0.0:5555
[服务器] 服务器启动成功,监听端口: 5555
[服务器] 按 'Q' 键停止服务器...
[服务器] 客户端连接: ID=1, 地址=127.0.0.1:12345
[服务器] SSL握手完成: ID=1
[服务器] 接收到消息: ID=1, 内容="hello from client"
[服务器] 发送响应: "hello!"
```
### 客户端输出示例:
```
========================================
HP-Socket SSL Client 控制台演示程序
========================================
[客户端] 正在初始化SSL环境...
[客户端] SSL环境初始化成功
[客户端] 正在连接服务器 127.0.0.1:5555 ...
[客户端] 连接成功: 本地地址=127.0.0.1:12345
[客户端] SSL握手完成
[客户端] 已连接到服务器,可以开始发送消息
----------------------------------------
命令菜单:
1 - 发送 'hello' 到服务器
Q - 断开连接并退出
----------------------------------------
请输入命令: 1
[客户端] 发送消息: "hello"
[客户端] 收到服务器响应: "hello!"
```
## SSL证书说明
本程序使用的SSL证书已经内嵌在代码中来自 `TestEcho-SSL` 示例项目):
- 服务器证书:`server.cer` / `server.key`
- 客户端证书:`client.cer` / `client.key`
- CA证书`ca.crt`
- 密钥密码123456
这些证书仅用于演示目的,**不应在生产环境中使用**。
## 技术细节
### 核心类和接口
- **CSSLServer**SSL服务器类
- **CSSLClient**SSL客户端类
- **CTcpServerListener**:服务器事件监听器接口
- **CTcpClientListener**:客户端事件监听器接口
### SSL配置
- **服务器验证模式**SSL_VM_PEER | SSL_VM_FAIL_IF_NO_PEER_CERT
- **客户端验证模式**SSL_VM_PEER
- **加密方式**使用内存中的PEM格式证书
### 依赖库
- `crypt32.lib` - Windows加密API
- `libssl.lib` - OpenSSL SSL/TLS库
- `libcrypto.lib` - OpenSSL加密库
- `ws2_32.lib` - Windows套接字库
## 故障排除
### 编译错误
1. **找不到OpenSSL头文件**
- 检查 `Windows/Dependent/openssl/v143/` 目录是否存在
- 确认项目属性中的包含目录设置正确
2. **链接错误找不到libssl.lib**
- 检查 `Windows/Dependent/openssl/v143/[x86|x64]/lib/` 目录
- 确认项目属性中的库目录设置正确
### 运行时错误
1. **服务器启动失败:端口已被占用**
- 检查端口5555是否被其他程序占用
- 使用 `netstat -ano | findstr 5555` 查看端口占用情况
2. **客户端连接失败**
- 确保服务器已经启动
- 检查防火墙设置
- 确认服务器监听的地址和端口正确
3. **SSL握手失败**
- 检查证书配置是否正确
- 确认OpenSSL DLL文件在系统路径中
## 相关链接
- HP-Socket项目https://github.com/ldcsaa/HP-Socket
- OpenSSL官网https://www.openssl.org/
## 版本信息
- HP-Socket版本6.0.7
- Visual Studio版本2022
- 平台工具集v143
- Windows SDK10.0
## 许可证
本演示程序遵循HP-Socket项目的Apache License 2.0许可证。