-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientSocket.cs
300 lines (242 loc) · 8.11 KB
/
ClientSocket.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Windows.Forms;
using System.Threading;
namespace Temperature_Monitor
{
public class ClientSocket
{
private TcpClient client;
private NetworkStream stream;
private int timeout = 10000; //The default timeout
bool just_connected = false;
public ClientSocket()
{
client = new TcpClient();
}
public int Timeout
{
get { return timeout; }
set { timeout = value; }
}
public string GetHostnameFromIp(string ipAddress)
{
try
{
IPHostEntry entry = Dns.GetHostEntry(ipAddress);
if (entry != null)
{
return entry.HostName;
}
}
catch (SocketException)
{
return null;
//unknown host or
//not every IP has a name
//log exception (manage it)
}
return null;
}
public bool Connect(String server, int port)
{
try
{
//client = new HttpClient();
//client.BaseAddress = new Uri("http://" + server + ":2000/");
//client.DefaultRequestHeaders.Accept.Clear();
//client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("-r -S "));
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
// Connect to the specified host.
client = new TcpClient();
//get IP addresses. 1st address is ip6, 2nd is ip4
//if there is only one address returned then it is ip4
IPAddress ip4;
IPAddress[] IPAddresses = Dns.GetHostAddresses(server);
if (IPAddresses.Length == 2)
{
ip4 = IPAddresses[1];
}
else ip4 = IPAddresses[0];
client.Connect(ip4, port);
just_connected = client.Connected;
return just_connected;
}
catch (SocketException e)
{
string error = e.ToString();
return false;
}
}
public bool Connect(IPAddress server, int port)
{
try
{
if (client != null)
{
client.Close(); //free up resources associated with the old socket connection before we form a new connection
}
client = new TcpClient();
IAsyncResult result = client.BeginConnect(server, port, null, null);
bool success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(20));
if (!success)
{
throw new SocketException();
}
// we have connected
client.EndConnect(result);
//client = new TcpClient();
//IPEndPoint ipEndPoint = new IPEndPoint(server, port);
//client.Connect(ipEndPoint);
just_connected = client.Connected;
return just_connected;
}
catch (SocketException e)
{
string error = e.ToString();
return false;
}
}
public bool IsConnected()
{
try
{
return client.Connected;
}
catch (SocketException)
{
return false;
}
catch (NullReferenceException)
{
return false;
}
}
public bool SendReceiveData(Byte[] data, ref string result)
{
try
{
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
client.SendTimeout = 1000;
if (client.Connected) stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
//Console.WriteLine("Sent: {0}", request);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
stream.ReadTimeout = timeout;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = 0;
bytes = stream.Read(data, 0, data.Length);
result = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
//Console.WriteLine("Received: {0}", responseData);
return true;
}
catch (ArgumentNullException e)
{
return false;
}
catch (System.IO.IOException e)
{
return false;
}
catch (InvalidOperationException e)
{
return false;
}
catch (TimeoutException)
{
return false;
}
}
public bool SendReceiveData(String request, ref string result,bool special_encoding)
{
try
{
client.SendTimeout = 1000;
if(client.Connected) stream = client.GetStream();
Byte[] data = new Byte[60];
Int32 bytes;
if (just_connected)
{
bytes = stream.Read(data, 0, data.Length); //read the connection string
just_connected = false;
}
// Translate the passed message into ASCII and store it as a Byte array.
data = System.Text.Encoding.ASCII.GetBytes(request);
stream.Write(data, 0, data.Length);
Thread.CurrentThread.Join(1000);
// Buffer to store the response bytes.
data = new Byte[60];
stream.ReadTimeout = timeout;
// stream.BeginRead(
// Read the first batch of the TcpServer response bytes.
bytes = stream.Read(data, 0, data.Length);
result = Encoding.ASCII.GetString(data, 0, bytes);
//Console.WriteLine("Received: {0}", responseData);
return true;
}
catch (ArgumentNullException)
{
return false;
}
catch (System.IO.IOException e)
{
return false;
}
catch (TimeoutException)
{
return false;
}
catch (InvalidOperationException e)
{
return false;
}
}
public bool CloseConnection()
{
try
{
// Close everything.
//stream.Close();
client.Close();
return true;
}
catch (SocketException)
{
return false;
}
}
public string GetHostName(string ipAddress)
{
try
{
IPHostEntry entry = Dns.GetHostEntry(ipAddress);
if (entry != null)
{
return entry.HostName;
}
}
catch (SocketException e)
{
//unknown host or
//not every IP has a name
//log exception (manage it)
}
return null;
}
public TcpClient GetClient
{
get { return client; }
}
}
}