1
- namespace TestClient
2
- {
3
- using System ;
4
- using System . Text ;
5
- using WatsonTcp ;
1
+ using System ;
2
+ using System . Text ;
3
+ using WatsonTcp ;
6
4
7
- internal class TestClient
5
+ namespace TestClient
6
+ {
7
+ class TestClient
8
8
{
9
- private static string serverIp = String . Empty ;
10
- private static int serverPort = 0 ;
11
- private static bool useSsl = false ;
12
- private static string certFile = String . Empty ;
13
- private static string certPass = String . Empty ;
14
- private static bool acceptInvalidCerts = true ;
15
- private static bool mutualAuthentication = true ;
16
- private static WatsonTcpClient client = null ;
17
- private static string presharedKey = null ;
18
-
19
- private static void Main ( )
9
+ static string serverIp = "" ;
10
+ static int serverPort = 0 ;
11
+ static bool useSsl = false ;
12
+ static string certFile = "" ;
13
+ static string certPass = "" ;
14
+ static bool acceptInvalidCerts = true ;
15
+ static bool mutualAuthentication = true ;
16
+ static WatsonTcpClient client = null ;
17
+ static string presharedKey = null ;
18
+
19
+ static void Main ( string [ ] args )
20
20
{
21
21
serverIp = Common . InputString ( "Server IP:" , "127.0.0.1" , false ) ;
22
22
serverPort = Common . InputInteger ( "Server port:" , 9000 , true , false ) ;
@@ -105,32 +105,21 @@ private static void Main()
105
105
}
106
106
else
107
107
{
108
- client = new WatsonTcpClient ( serverIp , serverPort )
109
- {
110
- ServerConnected = ServerConnected ,
111
- ServerDisconnected = ServerDisconnected ,
112
- MessageReceived = MessageReceived ,
113
- } ;
114
-
115
- client . Start ( ) ;
108
+ client = new WatsonTcpClient ( serverIp , serverPort ) ;
109
+ client . ServerConnected = ServerConnected ;
110
+ client . ServerDisconnected = ServerDisconnected ;
111
+ client . MessageReceived = MessageReceived ;
112
+ client . Start ( ) ;
116
113
}
117
-
118
114
break ;
119
115
120
116
case "reconnect" :
121
- if ( client != null )
122
- {
123
- client . Dispose ( ) ;
124
- }
125
-
126
- client = new WatsonTcpClient ( serverIp , serverPort )
127
- {
128
- ServerConnected = ServerConnected ,
129
- ServerDisconnected = ServerDisconnected ,
130
- MessageReceived = MessageReceived ,
131
- } ;
132
-
133
- client . Start ( ) ;
117
+ if ( client != null ) client . Dispose ( ) ;
118
+ client = new WatsonTcpClient ( serverIp , serverPort ) ;
119
+ client . ServerConnected = ServerConnected ;
120
+ client . ServerDisconnected = ServerDisconnected ;
121
+ client . MessageReceived = MessageReceived ;
122
+ client . Start ( ) ;
134
123
break ;
135
124
136
125
case "psk" :
@@ -152,36 +141,22 @@ private static void Main()
152
141
}
153
142
}
154
143
155
- private static void InitializeClient ( )
156
- {
144
+ static void InitializeClient ( )
145
+ {
157
146
if ( ! useSsl )
158
147
{
159
148
client = new WatsonTcpClient ( serverIp , serverPort ) ;
160
149
}
161
150
else
162
151
{
163
- bool provideCertificate = Common . InputBoolean ( "Do you wish to provide a certificate ? (required for mutual authenication)" , true ) ;
152
+ certFile = Common . InputString ( "Certificate file:" , "test.pfx" , false ) ;
153
+ certPass = Common . InputString ( "Certificate password:" , "password" , false ) ;
164
154
acceptInvalidCerts = Common . InputBoolean ( "Accept Invalid Certs:" , true ) ;
155
+ mutualAuthentication = Common . InputBoolean ( "Mutually authenticate:" , true ) ;
165
156
166
- if ( provideCertificate )
167
- {
168
- certFile = Common . InputString ( "Certificate file:" , "test.pfx" , false ) ;
169
- certPass = Common . InputString ( "Certificate password:" , "password" , false ) ;
170
- mutualAuthentication = Common . InputBoolean ( "Mutually authenticate:" , true ) ;
171
-
172
- client = new WatsonTcpClient ( serverIp , serverPort , certFile , certPass )
173
- {
174
- AcceptInvalidCertificates = acceptInvalidCerts ,
175
- MutuallyAuthenticate = mutualAuthentication ,
176
- } ;
177
- }
178
- else
179
- {
180
- client = new WatsonTcpClient ( Mode . Ssl , serverIp , serverPort , null )
181
- {
182
- AcceptInvalidCertificates = acceptInvalidCerts ,
183
- } ;
184
- }
157
+ client = new WatsonTcpClient ( serverIp , serverPort , certFile , certPass ) ;
158
+ client . AcceptInvalidCertificates = acceptInvalidCerts ;
159
+ client . MutuallyAuthenticate = mutualAuthentication ;
185
160
}
186
161
187
162
client . AuthenticationFailure = AuthenticationFailure ;
@@ -196,45 +171,41 @@ private static void InitializeClient()
196
171
client . Start ( ) ;
197
172
}
198
173
199
- private static string AuthenticationRequested ( )
174
+ static string AuthenticationRequested ( )
200
175
{
201
- Console . WriteLine ( String . Empty ) ;
202
- Console . WriteLine ( String . Empty ) ;
176
+ Console . WriteLine ( "" ) ;
177
+ Console . WriteLine ( "" ) ;
203
178
Console . WriteLine ( "Server requests authentication" ) ;
204
179
Console . WriteLine ( "Press ENTER and THEN enter your preshared key" ) ;
205
- if ( String . IsNullOrEmpty ( presharedKey ) )
206
- {
207
- presharedKey = Common . InputString ( "Preshared key:" , "1234567812345678" , false ) ;
208
- }
209
-
180
+ if ( String . IsNullOrEmpty ( presharedKey ) ) presharedKey = Common . InputString ( "Preshared key:" , "1234567812345678" , false ) ;
210
181
return presharedKey ;
211
182
}
212
183
213
- private static bool AuthenticationSucceeded ( )
184
+ static bool AuthenticationSucceeded ( )
214
185
{
215
186
Console . WriteLine ( "Authentication succeeded" ) ;
216
187
return true ;
217
188
}
218
189
219
- private static bool AuthenticationFailure ( )
190
+ static bool AuthenticationFailure ( )
220
191
{
221
192
Console . WriteLine ( "Authentication failed" ) ;
222
193
return true ;
223
194
}
224
195
225
- private static bool MessageReceived ( byte [ ] data )
196
+ static bool MessageReceived ( byte [ ] data )
226
197
{
227
198
Console . WriteLine ( "Message from server: " + Encoding . UTF8 . GetString ( data ) ) ;
228
199
return true ;
229
200
}
230
201
231
- private static bool ServerConnected ( )
202
+ static bool ServerConnected ( )
232
203
{
233
204
Console . WriteLine ( "Server connected" ) ;
234
205
return true ;
235
206
}
236
207
237
- private static bool ServerDisconnected ( )
208
+ static bool ServerDisconnected ( )
238
209
{
239
210
Console . WriteLine ( "Server disconnected" ) ;
240
211
return true ;
0 commit comments