TCPServer.cs
12.3 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Dolen.Communication.TCP
{
public class TCPServer
{
Thread threadWatch = null; // 负责监听客户端连接请求的线程;
Socket socketWatch = null;
int buffLength = 1024;// 定义一个1M的缓存区;
List<Client> Clients = new List<Client>();
//Dictionary<string, Socket> dict = new Dictionary<string, Socket>();
//Dictionary<string, Thread> dictThread = new Dictionary<string, Thread>();
log4net.ILog Log;
private bool loop = false;
/// <summary>
/// 消息变化委托
/// </summary>
/// <param name="clientKey">客户端信息</param>
/// <param name="msg">客户端发送的消息</param>
public delegate void MsgEventHandler(string clientKey, string msg);
public delegate void ClientChangedEventHandler(List<Client> clients);
public delegate void ServerStateChangedEventHandler(bool state);
/// <summary>
/// 消息接收事件 (string clientKey, string msg)
/// </summary>
public event MsgEventHandler RecvMsgEvent;
/// <summary>
/// 客户端变化事件 (List<string> clientKey)
/// </summary>
public event ClientChangedEventHandler ClientChangedEvent;
public event ServerStateChangedEventHandler ServerStateChangedEvent;
/// <summary>
/// 是否打开调试
/// </summary>
public bool IsDebug { get; set; } = false;
/// <summary>
/// 服务端IP
/// </summary>
public string IP { get; private set; }
/// <summary>
/// 服务端信息
/// </summary>
public string ServerInfo
{
get
{
return string.Format("{0}:{1}", IP, Port.ToString());
}
}
/// <summary>
/// 服务端端口
/// </summary>
public int Port { get; private set; }
/// <summary>
/// 启动状态
/// </summary>
public bool Started { get; private set; }
public TCPServer(string ip = "0.0.0.0", int port = 8888, string logName = "TCPServer")
{
Log = log4net.LogManager.GetLogger(logName);
IP = ip;
Port = port;
// 创建负责监听的套接字,注意其中的参数;
socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// 获得文本框中的IP对象;
IPAddress address = IPAddress.Parse(IP);
// 创建包含ip和端口号的网络节点对象;
IPEndPoint endPoint = new IPEndPoint(address, Port);
try
{
// 将负责监听的套接字绑定到唯一的ip和端口上;
socketWatch.Bind(endPoint);
}
catch (SocketException se)
{
Log.Error(string.Format("套接字绑定到服务端[{0}]出现异常", ServerInfo), se);
return;
}
// 设置监听队列的长度;
socketWatch.Listen(10);
// 创建负责监听的线程;
threadWatch = new Thread(WatchConnecting);
threadWatch.IsBackground = true;
threadWatch.Start();
}
~TCPServer()
{
try
{
socketWatch.Dispose();
}
catch { }
}
/// <summary>
/// 设置缓存区长度
/// </summary>
public void SetBuffLength(int len)
{
buffLength = len;
}
/// <summary>
/// 开启服务
/// </summary>
public void Start()
{
if (Started)
return;
loop = true;
Started = true;
Log.Info(string.Format("TCP服务端[{0}]开启监听成功!", ServerInfo));
ServerStateChangedEvent?.Invoke(Started);
}
public void Stop()
{
loop = false;
//foreach (string t in dictThread.Keys)
//{
// try
// {
// dictThread[t].Abort();
// }
// catch (Exception ex)
// {
// Log.Error(string.Format("关闭连接客户端[{0}]的线程失败", t), ex);
// }
//}
foreach (Client client in Clients)
{
try
{
client.RecvMsgThread.Abort();
}
catch (Exception ex)
{
Log.Error(string.Format("关闭连接客户端[{0}]的线程失败", client.EndPoint), ex);
}
try
{
client.Socket.Close();
}
catch (Exception ex)
{
Log.Error(string.Format("关闭连接客户端[{0}]Socket失败", client.EndPoint), ex);
}
}
if (Clients.Count > 0) Clients.Clear();
//foreach (string socket in dict.Keys)
//{
// try
// {
// dict[socket].Close();
// }
// catch (Exception ex)
// {
// Log.Error(string.Format("关闭连接客户端[{0}]Socket失败", socket), ex);
// }
//}
Started = false;
ServerStateChangedEvent?.Invoke(Started);
ClientChangedEvent?.Invoke(Clients);
Log.Info(string.Format("TCP服务端[{0}]关闭监听成功!", ServerInfo));
}
/// <summary>
/// 监听客户端请求的方法;
/// </summary>
void WatchConnecting()
{
while (true) // 持续不断的监听客户端的连接请求;
{
while (loop)
{
// 开始监听客户端连接请求,Accept方法会阻断当前的线程;
Socket sokConnection = socketWatch.Accept(); // 一旦监听到一个客户端的请求,就返回一个与该客户端通信的 套接字;
Client client = new Client();
client.EndPoint = sokConnection.RemoteEndPoint.ToString();
client.Socket = sokConnection;
if (CheckExistClient(sokConnection.RemoteEndPoint.ToString()))
{
Client remove = Clients.Find(s => s.IP.Equals(client.IP));
try
{
remove.RecvMsgThread.Abort();
remove.Socket.Close();
}
catch (Exception ex)
{
Log.Error("WatchConnecting", ex);
}
Clients.Remove(remove);
}
Log.Info(string.Format("客户端[{0}]连接服务端[{1}]成功", sokConnection.RemoteEndPoint.ToString(), ServerInfo));
Thread thr = new Thread(RecMsg);
thr.IsBackground = true;
thr.Start(sokConnection);
client.RecvMsgThread = thr;
Clients.Add(client);
ClientChangedEvent?.Invoke(Clients);
}
Thread.Sleep(5);
}
}
void RecMsg(object sokConnectionparn)
{
Socket sokClient = sokConnectionparn as Socket;
while (loop)
{
// 定义一个缓存区;
byte[] arrMsgRec = new byte[buffLength];
// 将接收到的数据存入到输入 arrMsgRec中;
int length = -1;
if (sokClient.Poll(-1, SelectMode.SelectRead))
{
try
{
length = sokClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度;
if (length == 0)//连接正常断开
{
Log.Info(string.Format("客户端[{0}]断开与服务端[{1}]的连接", sokClient.RemoteEndPoint.ToString(), ServerInfo));
// 从 通信套接字 集合中删除被中断连接的通信套接字;
RemoveClient(sokClient.RemoteEndPoint.ToString());
ClientChangedEvent?.Invoke(Clients);
break;
}
}
catch (SocketException se)
{
Log.Error(string.Format("客户端[{0}]接收服务端[{1}]的消息出现异常", sokClient.RemoteEndPoint.ToString(), ServerInfo), se);
// 从 通信套接字 集合中删除被中断连接的通信套接字;
RemoveClient(sokClient.RemoteEndPoint.ToString());
break;
}
catch (Exception e)
{
Log.Error(string.Format("客户端[{0}]接收服务端[{1}]的消息出现异常", sokClient.RemoteEndPoint.ToString(), ServerInfo), e);
RemoveClient(sokClient.RemoteEndPoint.ToString());
ClientChangedEvent?.Invoke(Clients);
break;
}
}
if (length > 0)
{
string strMsg = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, length);// 将接收到的字节数据转化成字符串;
//if (IsDebug)
Log.Info($"服务端[{ServerInfo}]收到客户端[{sokClient.RemoteEndPoint.ToString()}]的消息:{strMsg}");
//Log.Info(string.Format("服务端[{0}]收到客户端[{1}]的消息:{2}[{3} Bytes]", ServerInfo, sokClient.RemoteEndPoint.ToString(), strMsg,length));
RecvMsgEvent?.Invoke(sokClient.RemoteEndPoint.ToString(), strMsg);
}
Thread.Sleep(1);
}
}
/// <summary>
/// 向客户端发送信息
/// </summary>
public void SendMsg(string clientKey, string txt)
{
try
{
byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(txt); // 将要发送的字符串转换成Utf-8字节数组;
// arrSendMsg[0] = 0; // 表示发送的是消息数据
//Buffer.BlockCopy(arrMsg, 0, arrSendMsg, 1, arrMsg.Length);
int len = GetClient(clientKey).Socket.Send(arrMsg);
Log.Info($"服务端[{ServerInfo}]向客户端[{clientKey}]发送消息:{txt}");
}
catch (Exception e)
{
Log.Error(string.Format("服务端[{0}]向客户端[{1}]发送消息异常:{2}", ServerInfo, clientKey, txt), e);
RemoveClient(clientKey);
ClientChangedEvent?.Invoke(Clients);
}
}
bool CheckExistClient(string endpoint)
{
string ip = endpoint.Split(':')[0];
Client client = Clients.Find(s => s.IP.Equals(ip));
return client != null;
}
void RemoveClient(string endpoint)
{
Client client = Clients.Find(s => s.EndPoint.Equals(endpoint));
Clients.Remove(client);
}
Client GetClient(string endpoint)
{
return Clients.Find(s => s.EndPoint.Equals(endpoint));
}
}
public class Client
{
/// <summary>
/// 终端信息
/// </summary>
public string EndPoint { get; set; }
public string IP { get { return EndPoint.Split(':')[0]; } }
/// <summary>
/// 套接字
/// </summary>
public Socket Socket { get; set; }
/// <summary>
/// 接收的消息的线程
/// </summary>
public Thread RecvMsgThread { get; set; }
}
}