|
悬赏5金钱未解决
TCP服务端能正常连接,但是SSL服务端连接怎么都有问题,连接进入马上就断开了,错误代码133 ^9 G* C4 d1 P
/ J0 \" K& F2 l
0 {$ j* T# F- R, S% R; busing System;0 u1 D* E: S& N
using System.Threading.Tasks;+ \' e O. x0 I3 o
using HPSocket; D4 c+ n y$ W" t$ N
using HPSocket.Ssl;7 d' E0 n: Z4 u# y b# m
using HPSocket.Tcp;
; p; s# T/ Z9 F9 b0 K: n
$ ?3 K5 M* _+ ~, C! anamespace SslServerDemo+ i# R0 U7 _8 d7 P" d3 R& ~# d! O6 e
{
! W2 d0 k) E6 _4 W9 u" {2 G3 O class Program
+ w l' U1 C# w* H" v {/ I: P5 U% r, a7 P" G( {
static void Main(string[] args)% T; \& X# b4 l, ^
{' X- b7 M z4 B% `9 }. ?
// 创建SSL服务端; z, s: f) g& ~6 ^
//var server = new SslServer();
3 @8 G6 W1 O# T/ R9 A( B ISslServer server = new SslServer(); 8 [; {# q3 o% r
6 P; u. g8 t+ z$ ~+ t7 @, N# { // 设置证书和私钥, t& y' o! [- [8 `
' q2 t+ E4 [6 }. \8 |9 R4 V+ _ server.Initialize(false);
3 D$ n' n3 p! Z7 i( a4 B- I server.VerifyMode = SslVerifyMode.None;7 G; Z t" ~0 d& t
server.PemCertFile = "D:/cxiapu/zs/server.crt";
3 ^4 k/ N# B/ ], I4 x3 r8 N* v server.PemKeyFile = "D:/cxiapu/zs/server.key";0 r# M ~% _/ N- |) } ^/ k
server.KeyPassword = "mwyfeeq568";
8 ` Y- @& t* u2 l, a server.CaPemCertFileOrPath = "D:/cxiapu/zs/ca.crt";4 a6 u3 f+ X7 X, \1 v* o
- _$ O D) H0 o- W
//server.AddContext(SslVerifyMode.None, "D:/cxiapu/zs/server.crt", "D:/cxiapu/zs/server.key", "mwyfeeq568", "D:/cxiapu/zs/ca.crt");# [; O, w! u) Z& |" ]+ m9 v% N
server.Address = "0.0.0.0";- d' s; |) e0 D$ g9 }4 {- A6 \
server.Port = 5001;8 e4 h5 Y4 \4 r5 ^
0 }3 J$ ~& ` a5 [1 O7 i3 T
// 启动服务端
+ z: q, m! R |' \4 n# x Console.WriteLine(server.Start());
7 M& f7 _: @5 E P$ v3 ~ Console.WriteLine("SSL服务启动 127.0.0.1:5001");5 d, C9 u. A, s; l* b+ f. D
4 W$ a, o* _3 z+ i( T1 X* k, v
* |9 u' h7 W7 x$ \0 c1 n7 }$ _3 u) N# L/ B; w
! W. r/ j4 {( |
! ?7 A6 { g3 {; P9 Z$ b9 ^ // 设置事件回调% m; U% d0 S5 W- f- R' L8 `
) o; W y& Q m
server.OnSend += (sender, connId, data) =>
; M( F7 l" J; n+ s {" H F1 L& O. n! ?; X% k w) B
Console.WriteLine("发送事件");
( m0 }0 n+ |$ D/ e" G8 g! b
* q1 b" R8 _0 a2 v2 t
; B: o% Y/ f7 s' K, H return (0); 5 z$ Z3 U: @7 s# L
};2 S8 J9 q' N2 q* [" N1 y
5 }1 i+ J2 V! S- L8 J7 W
server.OnHandShake += (sender, connId) =># C& C" G; q" R5 k1 E) E
{
+ h) P* `6 P! H: E
( k" M; i; V; S- ^7 ` Console.WriteLine($"握手事件");1 @4 ?$ T+ r' H9 ?" A7 l. w
return (0);( b# s `; ~; D; B$ n5 w& \' @
};) ^1 `* p9 N) |. H
0 O/ Q- Y2 h! P3 J1 Q5 L) ~# {/ b. z
4 }) {: N* t* z* K, M4 h6 | server.OnAccept += (sender, connId, client) =>9 j6 H, Z' V- g; q
{# Z0 \: U" x( |" U: h9 B
Console.WriteLine($"客户进入: {connId}");
* M7 b2 u$ Y- a2 H5 c/ M/ F
: u+ y" Z. ?; M$ f$ }2 e) j // 发送欢迎消息给客户端+ H! d! |0 @' {$ f* s6 X5 o
+ |& b) F4 D; Q9 d T. n# W& `7 R+ b6 M6 w% q& p
//server.Send(connId, "Welcome to SSL server!");
" M# ~; S* H1 n0 P3 C0 ?; W return (0);+ R, [5 C1 M w4 Y7 {9 g
};; `" f" z) p0 s P3 E: J
server.OnReceive += (sender, connId, data) =>0 ^4 ?( v, ^6 \: U" }
{
; a$ i4 @4 q# X4 S Console.WriteLine($"数据到达: {connId}, data: {data}");( j6 C# s- g$ N- p+ e: L* r
return (0);: Y3 _7 k7 n6 a$ U
};' @* H$ r4 {+ |, P
server.OnClose += (sender, connId, operation, errorCode) =>
, ]% a- l# R3 s {
- ~" f. A9 c i, H% ~) p6 P Console.WriteLine($"客户断开:{connId}错误代码:{errorCode}");
! u% B8 b! p& r% j
0 n; B( J# X# t$ n, o; f" } return (0);
/ V9 F1 M* {* V2 Y' m- N };" r' y$ A1 ]& b$ O; c% @# e8 x8 s) n# B
, y. H4 i3 _1 o: O
9 u/ N- L- V0 m7 ^0 e# Q/ @, H
8 k3 @8 m1 [4 r' \5 [
8 E5 B# g% K( {7 N% ^. J 7 Z# ]: n+ G8 I/ e
$ e' [$ ]7 Z4 } W: @( Y* F' R // 等待客户端连接并收发数据
! u+ Z1 q1 t( N* k6 ^7 j/ L Console.ReadKey();* z4 O8 _+ V; A4 g- @; ~ y+ p
6 ?+ Y! f. ^1 j: Q
// 停止服务端
" f6 N- y6 B; d7 M- {9 \3 i server.Stop();
/ h8 ]3 S1 l; a$ Q( ?. K; J8 P }
( h$ \& ]0 [4 r5 \ }7 f$ D. R$ I; u+ r; B
}1 [9 l9 }! D; p
7 S; S5 U4 c" T# q
5 d( N) f% O) F: d2 K
|
|