-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTelnetClient.cs
202 lines (195 loc) · 6.1 KB
/
TelnetClient.cs
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace FlightSimulatorApp.Utilities
{
public class TelnetClient : ITelnet
{
private Socket sender;
private readonly Mutex mutex = new Mutex();
// Connect to a remote device.
public void Connect(string ip, int port)
{
try
{
//Parse the given string ip to an IPAddress Object.
IPAddress ipAddress = IPAddress.Parse(ip);
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Create a TCP/IP socket.
sender = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
// Connect the socket to the remote endpoint. Catch any errors.
try
{
// Set the timeout for synchronous receive methods to 10 seconds (10000 milliseconds.)
sender.ReceiveTimeout = 10000;
sender.Connect(remoteEP);
Debug.WriteLine("Socket connected to {0}", this.sender.RemoteEndPoint.ToString());
}
catch (ArgumentNullException ane)
{
//Debug.WriteLine("ArgumentNullException : {0}", ane.ToString());
throw ane;
}
catch (SocketException se)
{
// Debug.WriteLine("SocketException : {0}", se.ToString());
throw se;
}
catch (TimeoutException te)
{
// Debug.WriteLine("TimeoutException : {0}", te.ToString());
throw te;
}
catch (ObjectDisposedException ode)
{
// Debug.WriteLine("ODE exception : {0}", ode.ToString());
throw ode;
}
catch (Exception e)
{
// Debug.WriteLine("Unexpected exception : {0}", e.ToString());
throw e;
}
}
catch (Exception e)
{
// Debug.WriteLine(e.ToString());
throw e;
}
}
public void Disconnect()
{
try
{
// Release the socket.
sender.Shutdown(SocketShutdown.Both);
}
catch (ArgumentNullException)
{
}
catch (SocketException)
{
}
catch (ObjectDisposedException)
{
}
catch (Exception)
{
}
finally
{
sender.Close();
}
}
public string Read()
{
try
{
mutex.WaitOne();
// Data buffer for incoming data.
byte[] bytes = new byte[1024];
// Receive the response from the remote device.
int bytesRec = sender.Receive(bytes);
string strGotten = Encoding.ASCII.GetString(bytes, 0, bytesRec);
Debug.WriteLine("Echoed message = {0}", strGotten);
mutex.ReleaseMutex();
return strGotten;
}
catch (ArgumentNullException)
{
mutex.ReleaseMutex();
//If any exception was caught, return "0"
return "ERR";
}
catch (SocketException)
{
mutex.ReleaseMutex();
//If any exception was caught, return "0"
return "ERR";
}
catch (ObjectDisposedException)
{
mutex.ReleaseMutex();
return "ERR";
}
catch (Exception)
{
mutex.ReleaseMutex();
//If any exception was caught, return "0"
return "ERR";
}
}
public void Write(string commandStr)
{
try
{
mutex.WaitOne();
// Encode the data string into a byte array.
byte[] messageBytesArr = Encoding.ASCII.GetBytes(commandStr + "\n");
// Send the data through the socket.
int bytesSent = sender.Send(messageBytesArr);
}
catch (ArgumentNullException ane)
{
throw ane;
}
catch (SocketException se)
{
throw se;
}
catch (ObjectDisposedException oe)
{
throw oe;
}
catch (Exception e)
{
throw e;
}
finally
{
mutex.ReleaseMutex();
}
}
public bool IsSocketAvailableReading()
{
try
{
int microSeconds = 10000000;
return sender.Poll(microSeconds, SelectMode.SelectRead);
} catch (SocketException)
{
return false;
}
catch (ObjectDisposedException)
{
return false;
}
catch (Exception)
{
return false;
}
}
public bool IsSocketAvailableWriting()
{
try
{
int microSeconds = 10000000;
return sender.Poll(microSeconds, SelectMode.SelectWrite);
} catch (SocketException)
{
return false;
}
catch (ObjectDisposedException)
{
return false;
}
catch (Exception)
{
return false;
}
}
}
}