|
悬赏5金钱未解决
TCP服务端能正常连接,但是SSL服务端连接怎么都有问题,连接进入马上就断开了,错误代码13
r! p8 h& H9 r6 a) s2 W* n( N; Z& k
) `& \+ d) A' `7 @; H) {* |' [6 M# Husing System;2 f* H; z+ }# J4 u4 w. H8 J+ p: ]2 t4 r
using System.Threading.Tasks;( X' M( M+ U [/ K2 B5 y
using HPSocket;6 q+ Z; Q" t5 u1 C) [* \- T4 R
using HPSocket.Ssl;
5 m! ?% y* o3 s, ]- X( i0 tusing HPSocket.Tcp;
- ]- k. D5 A5 q/ w Y! Z3 k1 m6 E
namespace SslServerDemo9 U4 `- z, [ y' w6 }
{
5 H, l' S' w' X& z class Program
( a$ j7 c- Y$ R$ s5 ^ {+ R! o3 u# T0 X: _
static void Main(string[] args)) v( p2 E2 f% W. F6 I4 C; y( q
{
, p9 Y4 H- ^8 l# C; A( n$ l // 创建SSL服务端: ]( i, |% K" X
//var server = new SslServer();
$ D; A# t: a$ n3 v ISslServer server = new SslServer();
$ ]8 ?7 k3 `( @' L/ w2 l- M
* \, H& Z" A) C- Q // 设置证书和私钥
/ w- S% k/ g9 c( W0 z# k- J) A 9 H% R) m2 `# ?+ t
server.Initialize(false);
+ S! [ t2 u. G' i. O9 ] server.VerifyMode = SslVerifyMode.None;
3 w8 q! z; x2 q7 N server.PemCertFile = "D:/cxiapu/zs/server.crt";
* A/ g/ y* `6 e$ v5 u, Q server.PemKeyFile = "D:/cxiapu/zs/server.key";
$ x) K, h) k( [6 Q8 \ server.KeyPassword = "mwyfeeq568";$ E) @ P1 e0 j H2 @( E: C/ x
server.CaPemCertFileOrPath = "D:/cxiapu/zs/ca.crt";
" A/ O% I0 K# N p/ s
$ v4 x, e1 _% |2 ` //server.AddContext(SslVerifyMode.None, "D:/cxiapu/zs/server.crt", "D:/cxiapu/zs/server.key", "mwyfeeq568", "D:/cxiapu/zs/ca.crt");
0 p& `4 h O7 { server.Address = "0.0.0.0";' k% Y5 v; q/ M. ~: g. Y& }
server.Port = 5001;
% s0 b/ \' d5 s+ ^' l; Z/ @4 g+ _4 J( G
) P+ B" z$ B5 {$ c3 O& P4 ` // 启动服务端
& f G0 b$ ?- V2 X Console.WriteLine(server.Start());
) d A6 W4 J3 i Console.WriteLine("SSL服务启动 127.0.0.1:5001");
) P, A) [7 z, i1 C% C+ ^% A& ~! c7 c1 S$ D) `# C
7 f( [" @9 y( {1 f- v1 L3 y' U0 p) l
9 g" w5 k+ p* S5 E
. ~. c: P( b) l$ I3 U5 L5 B0 g# [
// 设置事件回调
: U+ Y: J6 ^1 y+ M( Q9 n# a/ H* O! W5 s( q$ }# R9 O4 `$ ]1 t
server.OnSend += (sender, connId, data) =>' m4 s* C- U& @$ Y
{3 \% @, A2 ^+ d" y4 }' s* f3 m
Console.WriteLine("发送事件");
( v4 x8 h9 d; h9 w) s6 }, e; g \7 I3 |2 t T7 U0 K$ `
) n! w% K" Q3 a+ F9 V; L return (0); " b8 A$ N) y/ O' S; I
};
: B* h- ^8 v1 P9 z) E* V+ W/ H
server.OnHandShake += (sender, connId) =>
" B$ ?% P- h4 c' f {& m: l( C9 W( h
- c6 o! q3 l3 c7 q0 N; M Console.WriteLine($"握手事件");8 R* Q" F7 g- B; x
return (0);! u4 v7 u: ^6 O0 q+ t5 u; n
};
4 l2 G' A6 r) T& r& Q5 D+ q7 m1 h( B" D5 k
1 ]6 D L' ?* `; X% T% }
server.OnAccept += (sender, connId, client) =>' i$ ^4 H! r! g6 o
{
+ T4 S' E2 g+ T/ N* n) F Console.WriteLine($"客户进入: {connId}");
6 } E6 _5 C( B( q
- r2 M8 O) ~8 t6 l5 c$ a4 H/ u. V // 发送欢迎消息给客户端
7 @+ N7 }: z9 B- M5 ~: u) v! J
+ y; z6 f% S# h
; D2 x- J+ U* n: j) T //server.Send(connId, "Welcome to SSL server!");+ M, u: M1 i2 y/ u5 s: L+ |
return (0);
: Z9 v2 s( m3 \$ p6 X8 T };( K9 q4 |9 o, _& o2 C7 R
server.OnReceive += (sender, connId, data) =>
, g, p7 _0 T/ [& X9 {# R {
* a1 y4 E/ M9 {0 \+ Q- m Console.WriteLine($"数据到达: {connId}, data: {data}");
+ u' L7 ^/ O) ^' Y3 y return (0);
% M* q5 V# {* d6 h# H; Z# _ };
6 U2 T. L) o- ^9 j server.OnClose += (sender, connId, operation, errorCode) =>2 E- |2 R" h8 Y9 h' T
{
0 ] H* I) P1 ] Console.WriteLine($"客户断开:{connId}错误代码:{errorCode}");
: p2 b6 e3 s- B( Q2 a' W
( K% C; C' j+ S/ c3 w5 t* m: \0 w" T return (0);
4 f# C7 a9 Y: u5 D };* X3 ?& t3 w8 l3 ^' B
0 I2 C3 {9 Z$ G" n; @* ^
* z0 [1 I& w& p" L4 p! I J
7 l: H5 M! W+ \- y* G: l0 T
; X; M7 n, c: h& w+ N ! \4 @- V' ~: g8 u
7 d+ _7 j5 O, X# o+ `; \ // 等待客户端连接并收发数据3 `' I+ }2 | a# j7 c$ q! ~
Console.ReadKey();
4 n, t" B2 J* \$ J* C* x( B0 |. H; P( t9 G) j; e! g4 h1 g
// 停止服务端+ K& |% l4 {0 b# b7 j$ A
server.Stop();
+ L {8 ]3 j) z5 ?( g }1 F) U3 O2 ^; H$ w* _2 H+ S
}: [ S; B* t9 n5 S% w+ U! P" h+ _. Q y
}- w2 a% N# G% m9 F1 r- _' F8 ^( N
* C; W; l% t6 Y
& v% i4 R1 F/ j' X8 T' f
|
|