forked from cc8848/Socket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cs
More file actions
92 lines (88 loc) · 2.56 KB
/
Copy pathClient.cs
File metadata and controls
92 lines (88 loc) · 2.56 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
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SocketLibrary
{
/// <summary>
/// Socket的客户端
/// </summary>
public class Client : SocketBase
{
/// <summary>
/// 超时时间
/// </summary>
public const int CONNECTTIMEOUT = 10;
private TcpClient client;
private IPAddress ipAddress;
private int port;
protected Thread _listenningClientThread;
private string _clientName;
/// <summary>
/// 连接的Key
/// </summary>
public string ClientName
{
get { return _clientName; }
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="ipaddress">地址</param>
/// <param name="port">端口</param>
public Client(string ipaddress, int port)
: this(IPAddress.Parse(ipaddress), port)
{
}
/// <summary>
///初始化
/// </summary>
/// <param name="ipaddress">地址</param>
/// <param name="port">端口</param>
public Client(IPAddress ipaddress, int port)
{
this.ipAddress = ipaddress;
this.port = port;
this._clientName = ipAddress + ":" + port;
}
/// <summary>
/// 打开链接
/// </summary>
public void StartClient()
{
this.StartListenAndSend();//开启父类的监听线程
_listenningClientThread = new Thread(new ThreadStart(Start));
_listenningClientThread.Start();
}
/// <summary>
/// 关闭连接并释放资源
/// </summary>
public void StopClient()
{
//缺少通知给服务端 自己主动关闭了
_listenningClientThread.Abort();
this.EndListenAndSend();
}
private void Start()
{
while (true)
{
if (!this.Connections.ContainsKey(this._clientName))
{
try
{
client = new TcpClient();
client.SendTimeout = CONNECTTIMEOUT;
client.ReceiveTimeout = CONNECTTIMEOUT;
client.Connect(ipAddress, port);
this._connections.TryAdd(this._clientName, new Connection(client, this._clientName));
}
catch (Exception e)
{ //定义连接失败事件
}
}
Thread.Sleep(200);
}
}
}
}