|
悬赏5金钱未解决
TCP服务端能正常连接,但是SSL服务端连接怎么都有问题,连接进入马上就断开了,错误代码13
2 l& _3 t0 }8 D7 K
" } d* @* F/ d3 h) r6 U! \; c$ t+ w5 E( |5 y
using System;1 w) Y+ n9 \. S2 m0 @" c, S
using System.Threading.Tasks;
* ]. `: e4 }# Qusing HPSocket;
! s8 [, i. z7 ~4 B8 X! Vusing HPSocket.Ssl;
]) q( g" ^! ~3 z3 W9 ?; l0 {3 p' rusing HPSocket.Tcp;5 R; ^2 g' G G! m( J, v: p
. z2 {" X% A6 f9 G$ t2 znamespace SslServerDemo- O. ^7 j$ t. ^% D
{' V3 w c: j7 d3 c# \# ]/ u
class Program
, r# T1 e7 u7 u9 d" K4 t9 G4 Z {$ f# E) e7 d7 h8 b1 {9 m w9 x( Y" \2 f
static void Main(string[] args)
" c2 i8 D7 l5 u: F7 R; t$ M. |$ B$ R {
' I# u( t7 l2 M6 o6 m) _ // 创建SSL服务端; e& [' E* G0 i( i& n4 Z- o
//var server = new SslServer();! g0 ?7 M- l q/ e
ISslServer server = new SslServer(); 5 ^+ l0 R; x: r) J, P- R
% F( A) R) \! `% t) E$ {5 r, L' b6 K& W
// 设置证书和私钥( g8 O0 [, Y" B% R6 ]% }, D
. k6 E& H) D6 J. v server.Initialize(false); r: }2 `" z! D( g$ e$ Q& Y* P
server.VerifyMode = SslVerifyMode.None;
" ^0 x: b7 {, v1 Q1 D. L server.PemCertFile = "D:/cxiapu/zs/server.crt";. {' p3 ?% U s$ Q
server.PemKeyFile = "D:/cxiapu/zs/server.key";! N1 h8 Y3 o( h/ U3 `, l% Q% H
server.KeyPassword = "mwyfeeq568";
# g* L' u1 q. F5 R: @: t server.CaPemCertFileOrPath = "D:/cxiapu/zs/ca.crt";* I/ H3 q+ j) [* ~& m6 g
$ Q# e! ^+ u, N6 _
//server.AddContext(SslVerifyMode.None, "D:/cxiapu/zs/server.crt", "D:/cxiapu/zs/server.key", "mwyfeeq568", "D:/cxiapu/zs/ca.crt");! L; @, n) E5 _9 `/ r7 F2 v
server.Address = "0.0.0.0";: a7 c/ Q: w T. P" Z
server.Port = 5001;# H' v% [0 V7 d2 Z( G( H
' k; x1 w3 T1 |. Y$ V% i
// 启动服务端. {" m$ ^) @& \1 [" F+ e
Console.WriteLine(server.Start());
. e1 J. Z: M) h U/ ^; g Console.WriteLine("SSL服务启动 127.0.0.1:5001");
& R, R" c7 }# e$ ^# r9 a D, U) d, ]4 W/ o
# g2 t" @" u/ y1 s
0 ?5 X5 V% |* l) U
7 K+ |! V5 d% t+ Z) _5 @- G$ a2 s/ U, ^ o5 p
// 设置事件回调
\/ k+ L$ r! d" Z) }, b
7 F) |" @8 P- N: o/ I) t9 b! q server.OnSend += (sender, connId, data) =>' D, ^) o- Y! K4 y p/ V5 Z; o- r% W
{
: Q) a) Z/ A# r) M4 c+ X, q Console.WriteLine("发送事件");$ L6 k* [9 ?# C) E+ Z
. S2 W; O9 t: I
: D: D7 v* T/ S6 w return (0); / ~. W4 {1 r5 k p; D8 j# x
};6 d6 m6 i! Z' T( X
$ k$ K& |2 F$ S0 W: [ [% v5 G
server.OnHandShake += (sender, connId) =>
# S, V7 d( [$ e. E! @/ D {4 M" h" u* I' T! v, y+ m; ?3 J
1 L$ b: I1 s1 r( @! n% Z9 ?; T) h
Console.WriteLine($"握手事件");) R1 k3 g: u* p) F
return (0);( B! z ~9 D1 ]1 m
};: g1 m% H! X" B: A( i, X
; A9 f$ @& q. r; h. |7 x
/ [5 A o& P z$ e( d6 g* X server.OnAccept += (sender, connId, client) =>
* x0 |' p& o+ g2 n8 q. a {# R( K( I; u; j( y
Console.WriteLine($"客户进入: {connId}");
$ S* d. S/ { r- _
) R X' f, Y4 C9 ]: M // 发送欢迎消息给客户端
: y% P8 _1 I) y8 }3 H% Z$ R2 X- U- t7 K- L8 S. i
/ Y! }! J4 }2 }2 _$ q //server.Send(connId, "Welcome to SSL server!");
2 V" W+ \; A* q" q return (0);, ]) ]: P$ Q$ V
};
! L3 H3 h: G6 H9 k: g7 Q+ o7 e7 j8 h server.OnReceive += (sender, connId, data) =>* p8 q0 \3 j/ y9 y7 G
{2 C) a' t9 J& J$ Y! X& I, Q+ @3 n
Console.WriteLine($"数据到达: {connId}, data: {data}");
7 v5 ^! ^" K9 s" e' _( _* k return (0);5 Q5 g" ]& U0 `2 s1 q
};% U: R& q: J* r3 b5 A
server.OnClose += (sender, connId, operation, errorCode) =>
" S4 }; ]+ w9 M+ c( y: ? {
Z5 G9 W& ]! K2 L' l. i Console.WriteLine($"客户断开:{connId}错误代码:{errorCode}");5 q. C. A) i. g0 r! C5 j
1 M4 o& H' _3 p2 Y, g return (0);# u/ B; x0 ]: b- D) t
};! l6 z' M& O M, }
! V$ l# ^3 H5 |7 B/ R
$ G' n; ]9 _0 w0 u
+ b0 [) V8 c' _- |- y! p& y5 g0 x
2 e4 J) m* [. R0 k " X; h1 Y6 y* Q' K
5 _9 J0 a4 p+ j3 H: I' b A // 等待客户端连接并收发数据
$ i0 G4 p7 K' A a$ i* a Console.ReadKey();, h$ X3 z2 T" q, N9 j/ h
$ W E& p" N2 U
// 停止服务端# d7 ?2 w& f% `/ [( c5 _- G9 d4 t
server.Stop();
$ x3 J9 k$ v9 _6 B0 _" [% G }
C) n5 K: ~4 B' g4 z, [ }/ g) D4 @# d4 J7 U) X# m
}$ C6 w y8 x) k1 Q/ Q3 I
$ V8 X, g N3 X( i
8 L- Q+ H* A: ]
|
|