wxr
2020-11-12 a715181089be0d31cd737a5367ffd02690b9d77f
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
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
 
namespace HDL_ON.DriverLayer
{
    public class Control_TcpClient
    {
        //声明IP,端口,和一个用来连接的Socket
        public string _ip;
 
        private TcpClient _tcpClient;
 
        //创建一个委托,用来满足其他类调用
        //public delegate void DelegateMessage(string str);
        public Action<string> ReceiveEvent;
 
        /// <summary>
        /// 构造函数
        /// </summary>
        public Control_TcpClient(string serverIp)
        {
            _ip = serverIp;
        }
 
        //TCP连接
        public bool Connect(int _port = 8586)
        {
            if (string.IsNullOrEmpty(_ip) || _port == 0)
            {
                return false;
            }
            _tcpClient = new TcpClient();
            try
            {
                _tcpClient.Connect(IPAddress.Parse(_ip), _port);
                Task.Run(new Action(ReceiveMessage));//开启线程,不停接收消息
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            return true;//返回连接状态
        }
        /// <summary>
        /// 关闭连接
        /// </summary>
        /// <returns></returns>
        public bool Close()
        {
            if (_tcpClient == null)
                return true;
            _tcpClient.Close();
            _tcpClient = null;
            return true;
        }
 
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="bytes">需要发送的字节</param>
        public void SendMessage(byte[] bytes)
        {
            NetworkStream networkStream = _tcpClient.GetStream();
            networkStream.Write(bytes, 0, bytes.Length);
        }
 
        /// <summary>
        /// 获取列表数据回调方法
        /// </summary>
        public Action<string> GetListResponseAction;
        //接收消息
        public void ReceiveMessage()
        {
            NetworkStream networkStream = _tcpClient.GetStream();
            while (true)
            {
                // 定义一个2M的缓存区;
                byte[] arrMsgRec = new byte[1024 * 1024 * 2];
                int size = networkStream.Read(arrMsgRec, 0, arrMsgRec.Length);
                var tcpDataString = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, arrMsgRec.Length);
                if (!string.IsNullOrEmpty(tcpDataString))
                {
                    ReceiveEvent?.Invoke(tcpDataString);
                }
 
                //// 将接受到的数据存入到输入  arrMsgRec中;
                //int length = -1;
                //try
                //{
                //    length = socketClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度;
                //}
                //catch (Exception ex)
                //{
                //    MainPage.Log($"tcpListener  error 1 :  {ex.Message}");
 
                //    Flag_Receive = false;
                //    // 从通信线程集合中删除被中断连接的通信线程对象;
                //    string keystr = socketClient.RemoteEndPoint.ToString();
                //    dic_ClientSocket.Remove(keystr);//删除客户端字典中该socket
                //    dic_ClientThread[keystr].Abort();//关闭线程
                //    dic_ClientThread.Remove(keystr);//删除字典中该线程
 
                //    tcpClient = null;
                //    socketClient = null;
                //    break;
                //}
                //byte[] buf = new byte[length];
                //Array.Copy(arrMsgRec, buf, length);
                //lock (tcpClient.m_Buffer)
                //{
                //    var tcpDataString = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, length);
                //    if (!string.IsNullOrEmpty(tcpDataString))
                //    {
                //        ReceiveEvent?.Invoke(tcpDataString);
                //    }
 
                //    MainPage.Log($"接收服务端数据:{tcpDataString}");
                //}
            }
        }
 
    }
}