Client.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7. using System.Net;
  8. using System.Configuration;
  9. using System.Data.SqlClient;
  10. using AGV_WPF_Global;
  11. using System.Windows.Media;
  12. namespace AGV_WPF.Services
  13. {
  14. public class Client
  15. {
  16. public Client(string ip,int port)
  17. {
  18. ServerIP = ip;
  19. ServerPort = port;
  20. ComClient = this;
  21. }
  22. private string serverIP;
  23. public static Client ComClient;
  24. public string ServerIP
  25. {
  26. get { return serverIP; }
  27. set
  28. {
  29. if(serverIP != value)
  30. serverIP = value;
  31. }
  32. }
  33. private int serverPort;
  34. public int ServerPort
  35. {
  36. get { return serverPort; }
  37. set
  38. {
  39. if (serverPort != value)
  40. {
  41. serverPort = value;
  42. }
  43. }
  44. }
  45. public TcpClient _client = null;
  46. public Thread receiveThread = null;
  47. public Thread sendThread = null;
  48. public static bool DisLinkFlag = false;//断开连接标志
  49. public static event EventHandler LinkStatusChanged;
  50. public static string databaseStr;
  51. public static bool SuccessFalg = false;
  52. public delegate void InvokeDelegate();
  53. public void Connect()
  54. {
  55. try
  56. {
  57. if (_client == null)
  58. _client = new TcpClient(ServerIP, ServerPort);
  59. receiveThread = new Thread(ReceiveData);
  60. receiveThread.IsBackground = true;
  61. receiveThread.Start(_client);
  62. /*sendThread = new Thread(SendData);
  63. sendThread.IsBackground = true;*/
  64. }
  65. catch (System.Exception ex)
  66. {
  67. throw new Exception(ex.Message);
  68. }
  69. }
  70. public void ReceiveData(object o)
  71. {
  72. TcpClient client = o as TcpClient;
  73. if (client == null)
  74. return;
  75. int i = 0;
  76. byte[] buffer = new byte[1024];
  77. while (true)
  78. {
  79. if (client == null)
  80. break;
  81. if (client.Client.Poll(-1, SelectMode.SelectRead))
  82. {
  83. try
  84. {
  85. i = client.Client.Receive(buffer);
  86. DisLinkFlag = false;
  87. if (LinkStatusChanged != null)
  88. {
  89. LinkStatusChanged(this, null);
  90. }
  91. }
  92. catch (System.Net.Sockets.SocketException ex)
  93. {
  94. SuccessFalg = false;
  95. while (true)
  96. {
  97. try
  98. {
  99. if (MainWindow.mainWindow != null)
  100. {
  101. //MainWindow.mainWindow.UpdateLinkStatus("重连中...", Brushes.Red);
  102. }
  103. _client = new TcpClient(ServerIP, ServerPort);
  104. receiveThread = new Thread(ReceiveData);
  105. receiveThread.Start(_client);
  106. break;
  107. }
  108. catch (System.Exception ex2)
  109. {
  110. if (MainWindow.mainWindow != null)
  111. {
  112. //MainWindow.mainWindow.UpdateLinkStatus("重连失败", Brushes.Red);
  113. }
  114. Thread.Sleep(500);
  115. continue;
  116. }
  117. }
  118. Console.WriteLine("服务器关闭");
  119. DisLinkFlag = true;
  120. if (LinkStatusChanged != null)
  121. {
  122. LinkStatusChanged(this, null);
  123. }
  124. break;
  125. }
  126. if (i == 0)
  127. {
  128. //MessageBox.Show("服务器已关闭");
  129. Console.WriteLine("服务器关闭");
  130. DisLinkFlag = true;
  131. if (LinkStatusChanged != null)
  132. {
  133. LinkStatusChanged(this, null);
  134. }
  135. break;
  136. }
  137. else if (i > 0)
  138. {
  139. string str = System.Text.Encoding.Default.GetString(buffer, 0, i);
  140. if(str.StartsWith("#") && str.EndsWith("#"))
  141. {
  142. str = str.Replace("#","");
  143. Console.WriteLine(str);
  144. //string strCon = string.Format("Data Source={0};DataBase={1};user={2};pwd={3}", ServerIP, str, GlobalPara.DataBaseUser, GlobalPara.DataBasePwd);
  145. if (!SuccessFalg)
  146. {
  147. try
  148. {
  149. //using (SqlConnection con = new SqlConnection(strCon))
  150. {
  151. //con.Open();
  152. SuccessFalg = true;
  153. if (MainWindow.mainWindow != null)
  154. {
  155. //MainWindow.mainWindow.UpdateBackground();
  156. }
  157. Console.WriteLine("登录成功");
  158. //databaseStr = strCon;
  159. //con.Close();
  160. }
  161. }
  162. catch (System.Exception ex)
  163. {
  164. SuccessFalg = false;
  165. throw new Exception(ex.Message);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. Console.WriteLine("退出接收线程");
  173. }
  174. public void SendData(object o)
  175. {
  176. TcpClient client = o as TcpClient;
  177. while (true)
  178. {
  179. if (sendThread != null)
  180. {
  181. string str="";
  182. if (!string.IsNullOrEmpty(str))
  183. {
  184. byte[] buffer = System.Text.Encoding.Default.GetBytes(str);
  185. try
  186. {
  187. client.Client.Send(buffer);
  188. }
  189. catch (System.Exception ex)
  190. {
  191. Console.WriteLine(ex.Message);
  192. break;
  193. }
  194. }
  195. Thread.Sleep(500);
  196. }
  197. }
  198. }
  199. public void Close()
  200. {
  201. if (receiveThread != null)
  202. {
  203. receiveThread.Abort();
  204. receiveThread = null;
  205. }
  206. if (sendThread != null)
  207. {
  208. sendThread.Abort();
  209. sendThread = null;
  210. }
  211. if (_client != null)
  212. {
  213. _client = null;
  214. }
  215. }
  216. }
  217. }