TCPClient.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace FileAnalysis
  9. {
  10. class TCPClient
  11. {
  12. private Socket socket;
  13. private IPEndPoint serverFullAddr;//完整终端地址
  14. private bool isOpen = false;
  15. private bool receiveData;
  16. private DataHelper dh;
  17. private Dictionary<string, string> returnvalue = new Dictionary<string, string>();
  18. public bool IsOpen
  19. {
  20. get
  21. {
  22. return isOpen;
  23. }
  24. set
  25. {
  26. isOpen = value;
  27. }
  28. }
  29. public string IP
  30. {
  31. get
  32. {
  33. return iP;
  34. }
  35. set
  36. {
  37. iP = value;
  38. }
  39. }
  40. public string Port
  41. {
  42. get
  43. {
  44. return port;
  45. }
  46. set
  47. {
  48. port = value;
  49. }
  50. }
  51. public bool ReceiveData
  52. {
  53. get
  54. {
  55. return receiveData;
  56. }
  57. set
  58. {
  59. receiveData = value;
  60. }
  61. }
  62. public Dictionary<string, string> Returnvalue
  63. {
  64. get
  65. {
  66. return returnvalue;
  67. }
  68. set
  69. {
  70. returnvalue = value;
  71. }
  72. }
  73. internal DataHelper Dh
  74. {
  75. get
  76. {
  77. return dh;
  78. }
  79. set
  80. {
  81. dh = value;
  82. }
  83. }
  84. private string iP;
  85. private string port;
  86. private int ResetCount = 0;
  87. string decode = "";
  88. string dename = "";
  89. public TCPClient(string IP, string Port, string Decode, string Dename)
  90. {
  91. try
  92. {
  93. decode = Decode;
  94. dename = Dename;
  95. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  96. serverFullAddr = new IPEndPoint(IPAddress.Parse(IP), int.Parse(Port));//设置IP,端口
  97. socket.ReceiveTimeout = 2000;
  98. socket.Connect(serverFullAddr);
  99. }
  100. catch (Exception ex)
  101. {
  102. }
  103. }
  104. public void Send(string Command)
  105. {
  106. string str = "";
  107. try
  108. {
  109. if (!socket.Connected || ResetCount == 20)
  110. {
  111. socket.Close();
  112. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  113. socket.ReceiveTimeout = 2000;
  114. socket.Connect(serverFullAddr);
  115. ResetCount = 0;
  116. }
  117. byte[] arr = Encoding.ASCII.GetBytes(Command);
  118. //Command = Command.Replace(" ", "");
  119. //byte[] arr = new byte[Command.Length / 2];
  120. //for (int i = 0; i < Command.Length / 2; i++)
  121. //{
  122. // arr[i] = (byte)Convert.(Command.Substring(i * 2, 2), 16);
  123. //}
  124. socket.Send(arr);
  125. //byte[] receive = new byte[1024 * 10];
  126. //int length = socket.Receive(receive);
  127. //str = ByteToHexadecimalString(receive, length);
  128. //if (!returnvalue.ContainsKey(socket.RemoteEndPoint.ToString()))
  129. //{
  130. // returnvalue.Add(socket.RemoteEndPoint.ToString(), str);
  131. //}
  132. //ResetCount = ResetCount + 1;
  133. }
  134. catch (Exception ex)
  135. {
  136. Console.WriteLine(ex.Message + ex.StackTrace + "\n" + decode + " " + str);
  137. }
  138. }
  139. public static string ByteToHexadecimalString(byte[] b, int length)
  140. {
  141. string returnStr = "";
  142. if (b != null)
  143. {
  144. for (int i = 0; i < length; i++)
  145. {
  146. returnStr += Convert.ToString(b[i], 16);//ToString("X2") 为C#中的字符串格式控制符
  147. }
  148. }
  149. return returnStr.ToUpper();
  150. }
  151. }
  152. }