forked from chronoxor/NetCoreServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
116 lines (94 loc) · 3.4 KB
/
Copy pathProgram.cs
File metadata and controls
116 lines (94 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using NetCoreServer;
namespace SslChatClient
{
class ChatClient : SslClient
{
public ChatClient(SslContext context, string address, int port) : base(context, address, port) {}
public void DisconnectAndStop()
{
_stop = true;
DisconnectAsync();
while (IsConnected)
Thread.Yield();
}
protected override void OnConnected()
{
Console.WriteLine($"Chat SSL client connected a new session with Id {Id}");
}
protected override void OnHandshaked()
{
Console.WriteLine($"Chat SSL client handshaked a new session with Id {Id}");
}
protected override void OnDisconnected()
{
Console.WriteLine($"Chat SSL client disconnected a session with Id {Id}");
// Wait for a while...
Thread.Sleep(1000);
// Try to connect again
if (!_stop)
ConnectAsync();
}
protected override void OnReceived(byte[] buffer, long offset, long size)
{
Console.WriteLine(Encoding.UTF8.GetString(buffer, (int)offset, (int)size));
}
protected override void OnError(SocketError error)
{
Console.WriteLine($"Chat SSL client caught an error with code {error}");
}
private bool _stop;
}
class Program
{
static void Main(string[] args)
{
// SSL server address
string address = "127.0.0.1";
if (args.Length > 0)
address = args[0];
// SSL server port
int port = 2222;
if (args.Length > 1)
port = int.Parse(args[1]);
Console.WriteLine($"SSL server address: {address}");
Console.WriteLine($"SSL server port: {port}");
Console.WriteLine();
// Create and prepare a new SSL client context
var context = new SslContext(SslProtocols.Tls13, new X509Certificate2("client.pfx", "qwerty"), (sender, certificate, chain, sslPolicyErrors) => true);
// Create a new SSL chat client
var client = new ChatClient(context, address, port);
// Connect the client
Console.Write("Client connecting...");
client.ConnectAsync();
Console.WriteLine("Done!");
Console.WriteLine("Press Enter to stop the client or '!' to reconnect the client...");
// Perform text input
for (;;)
{
string line = Console.ReadLine();
if (string.IsNullOrEmpty(line))
break;
// Disconnect the client
if (line == "!")
{
Console.Write("Client disconnecting...");
client.DisconnectAsync();
Console.WriteLine("Done!");
continue;
}
// Send the entered text to the chat server
client.SendAsync(line);
}
// Disconnect the client
Console.Write("Client disconnecting...");
client.DisconnectAndStop();
Console.WriteLine("Done!");
}
}
}