| | |
| | | { |
| | | public class Control_TcpClient |
| | | { |
| | | /// <summary> |
| | | /// 通讯ID |
| | | /// </summary> |
| | | public const int msg_id = 1; |
| | | |
| | | //声明IP,端口,和一个用来连接的Socket |
| | | public string _ip; |
| | |
| | | { |
| | | _tcpClient.Connect(IPAddress.Parse(_ip), _port); |
| | | Task.Run(new Action(ReceiveMessage));//开启线程,不停接收消息 |
| | | MainPage.Log($"打开tcp client{_ip}:{_port}"); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | Console.WriteLine(e); |
| | | MainPage.Log(e.Message); |
| | | throw; |
| | | } |
| | | return true;//返回连接状态 |
| | |
| | | public void SendMessage(byte[] bytes) |
| | | { |
| | | NetworkStream networkStream = _tcpClient.GetStream(); |
| | | networkStream.Write(bytes, 0, bytes.Length); |
| | | if (networkStream.CanWrite) |
| | | { |
| | | networkStream.Write(bytes, 0, bytes.Length); |
| | | } |
| | | //networkStream.Close(); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | |
| | | // MainPage.Log($"接收服务端数据:{tcpDataString}"); |
| | | //} |
| | | |
| | | } |
| | | } |
| | | |
| | | |
| | | #region A协议 |
| | | /// <summary> |
| | | /// 发送A协议数据 |
| | | /// </summary> |
| | | /// <param name="topic"></param> |
| | | public void SendData(string topic) |
| | | { |
| | | string topicString = "Topic:" + topic + "/r/n"; |
| | | string lenghtString = "Length:" + "/r/n" + "/r/n"; |
| | | } |
| | | |
| | | #endregion |
| | | } |
| | | } |