|
悬赏5金钱未解决
TCP服务端能正常连接,但是SSL服务端连接怎么都有问题,连接进入马上就断开了,错误代码13
+ U0 t) D/ Q; b. E, {6 v( l/ z8 R' F& o: g
" y/ L! X8 _% b8 Z0 w) _using System;
* p+ P n# b) b- x+ o( i; p" |using System.Threading.Tasks;
' S# J: }/ o1 C2 m) Y) c5 yusing HPSocket;1 U$ f' x. A* _# l; I
using HPSocket.Ssl;
0 E" A1 h& ]: s |$ _" L6 ?$ Pusing HPSocket.Tcp;* V8 _1 v; f# s
e/ J5 f# ]) R5 V' Snamespace SslServerDemo: e6 ?+ \+ b* s5 N5 h
{
0 Z5 M, z* X1 v- M' M# A class Program
! i/ i; f* p2 s. |$ ~% U/ [ {9 S ?8 Q% t; I. }
static void Main(string[] args)& i$ V0 m0 j/ L8 \: {, C# b
{( L* F" R6 J" n1 t* p8 H% R6 I
// 创建SSL服务端. a3 T- p# D4 Z! H$ d7 c# S$ b
//var server = new SslServer();! p+ E1 o# ^& K3 ?
ISslServer server = new SslServer();
7 {0 w' V. T' n* M& v& Q% [: j; P
// 设置证书和私钥3 |; n! A* z; i- V8 {) |2 ^
# W+ L( t0 a! c* D0 i server.Initialize(false);" f1 {) N- K- b
server.VerifyMode = SslVerifyMode.None;
! _! S' P' w0 e0 m server.PemCertFile = "D:/cxiapu/zs/server.crt";/ ?2 t/ G+ n2 a, u) F! W, k
server.PemKeyFile = "D:/cxiapu/zs/server.key";$ G8 y m$ s4 z4 v
server.KeyPassword = "mwyfeeq568";+ e& G* @0 H/ V. ^5 R% e: |$ E
server.CaPemCertFileOrPath = "D:/cxiapu/zs/ca.crt";
% n8 y, _% q8 v
3 X l3 Y3 p' P8 ^( N; @ //server.AddContext(SslVerifyMode.None, "D:/cxiapu/zs/server.crt", "D:/cxiapu/zs/server.key", "mwyfeeq568", "D:/cxiapu/zs/ca.crt");% C4 m7 W2 U2 O( _; |
server.Address = "0.0.0.0";/ ]. q6 ~2 Q" V# ]
server.Port = 5001;
- _, a4 J5 p& V/ C3 c4 G" ?& O& o1 ?. J4 k, l0 l0 W
// 启动服务端; o5 d6 b0 ^0 }. _3 v
Console.WriteLine(server.Start());
: @ y% ?3 e& _ Console.WriteLine("SSL服务启动 127.0.0.1:5001");
4 U" I9 U8 v! a6 v) y. s
4 e H* j% K) D* z
5 N0 N l# G' G0 [& n {
" w8 B8 c/ h( B8 m+ z* D( k& F. p* X: e4 _- a6 p
: s* E+ l) R5 n& y% B G
// 设置事件回调
: z+ ^* [2 w9 d. s. p+ K* _7 {4 W; ~' r
server.OnSend += (sender, connId, data) =>
% e0 l) d. ?& \$ _9 m {
' X$ ^$ I: ~5 r0 W g' [; e4 E3 @; x Console.WriteLine("发送事件");
5 t& v, z$ x& e) R" a3 G- x5 B
; {0 q* ^8 x2 t; O5 M' M. h% l0 m! F
return (0);
! k; R9 L9 ~9 P x };9 C( V4 d' S: z# u8 i. ]% ]& d+ o
- ^ E9 U5 F; i$ V7 A/ V. l/ n+ ] server.OnHandShake += (sender, connId) =>7 U, o% _% _# Z% {: A7 O. G
{
. I& J1 ^: b4 W, I8 j/ Q, n G* X: B' z7 D: h6 u- A k7 j; o
Console.WriteLine($"握手事件");0 P4 E1 v& A9 I! O& D0 h% g$ Q1 p& T
return (0);6 b; d; y- I. M1 G. H: [! z
};/ T9 P. f& n+ @) ~+ T! k5 N% b1 c
/ ~" V v& i5 Q& |$ t: P
2 F7 \. w( u, y8 P7 A5 V server.OnAccept += (sender, connId, client) =>5 K* i2 I$ ]2 `- T
{
( j7 e# r5 b- l `% @1 j Console.WriteLine($"客户进入: {connId}");) ^. h6 k% ?0 Q3 A
8 Q* h( t- @# p* Q
// 发送欢迎消息给客户端
% {: u* N- m+ \4 C
0 b( k* @6 D0 I2 C& h. M. M7 J) f/ W* [2 |! j, S" Z
//server.Send(connId, "Welcome to SSL server!");5 j+ I/ Y2 J( z
return (0);
/ W; d! @) H1 P- s };
$ `3 X- h( |) V3 l server.OnReceive += (sender, connId, data) =>( Q4 U; @: I& o& U
{
3 x7 t% m# }" F4 j/ K* j# c Console.WriteLine($"数据到达: {connId}, data: {data}");0 l/ Y: @; b1 _1 A9 J9 D; t& H& c
return (0);) T! e# c7 E1 E
};8 E, A$ P8 v& r
server.OnClose += (sender, connId, operation, errorCode) =>
8 [$ o- y% x- a* H/ I9 l {
# U3 C8 w- t7 v& X) ~. O' h Console.WriteLine($"客户断开:{connId}错误代码:{errorCode}");" T6 J( V& ]) b0 U+ m
- p/ J0 ?9 ]2 K. ? @4 V1 n$ U3 d return (0);! L# ^4 e& Y+ w, K
};
3 t6 }# T# ^# f! L! I" \. H5 Z7 g6 Q |8 Y; R. _
% |9 h9 k% H" }) s. T
" W. @1 T5 Y5 z8 m2 z( j) s( r3 B& N% R1 a! @+ D
1 V; t( X# \- o9 |- Y
# @# r2 N7 v5 y: K
// 等待客户端连接并收发数据
, t, b6 [ l# n$ A4 }! l$ G8 H Console.ReadKey();5 \( o( H9 \5 q9 j: ~
8 H# j" r0 B; | // 停止服务端8 {, B/ s5 V7 W( ~
server.Stop();8 _1 u7 M' `; T; T& }7 ]3 [, l
}* D, |" X Y( n! ?
}, r7 F" q$ N/ L6 A4 K
}& q( ]4 N$ j$ G- ~. r; u
9 c0 s- A$ \: T4 q
6 }2 ?( V8 a" q: G$ `5 b |
|