ConSocket.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using DllFileSoc;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Windows.Forms;
  13. namespace FileToMD5
  14. {
  15. public partial class ConSocket : Form
  16. {
  17. Socket filesocket = null;
  18. public ConSocket()
  19. {
  20. InitializeComponent();
  21. }
  22. private void button1_Click(object sender, EventArgs e)
  23. {
  24. string errorinfo = string.Empty;
  25. filesocket = ConnServer(ref errorinfo);
  26. lblend.Text = errorinfo;
  27. TimerSatrt();
  28. }
  29. public void TimerSatrt()
  30. {
  31. System.Timers.Timer timer = new System.Timers.Timer();
  32. timer.Enabled = true;
  33. //timer.Interval = 1000*60*60;//执行间隔时间 1小时一次
  34. timer.Interval = 1000 * 30;
  35. timer.Start();
  36. timer.Elapsed += new System.Timers.ElapsedEventHandler(CheckRealIP);
  37. }
  38. public void CheckRealIP(object sender, System.Timers.ElapsedEventArgs e)
  39. {
  40. string errorinfo = string.Empty;
  41. try
  42. {
  43. byte[] datas = GetFileDatas(5888, 1, "");
  44. filesocket.Send(datas);
  45. int alllen = 0;
  46. datas = ReadData(filesocket, ref alllen, ref errorinfo);//接收反馈包
  47. if (datas == null)
  48. {
  49. LogBLL.Err(errorinfo);
  50. }
  51. else
  52. {
  53. byte[] statusdatas = new byte[4];
  54. Array.Copy(datas, 8, statusdatas, 0, 4);
  55. int status = BitConverter.ToInt32(statusdatas, 0);
  56. byte[] getinfo = new byte[datas.Length - 12];
  57. Array.Copy(datas, 12, getinfo, 0, getinfo.Length);
  58. string info = Encoding.UTF8.GetString(getinfo);
  59. LogBLL.Err(info);
  60. //if (status == 1)//根据MAC更新RealIP成功,关闭通信
  61. //{
  62. // if (filesocket!=null)
  63. // filesocket.Close();
  64. //}
  65. if (filesocket != null)
  66. filesocket.Close();
  67. //else//更新失败,使用之前的方法(根据机台ID更新RealIP)
  68. //{
  69. // if (!CheckIP(int.Parse(PubInfo.MacID), ref errorinfo))
  70. // {
  71. // LOG.log(errorinfo);
  72. // }
  73. // if (filesocket != null)
  74. // filesocket.Close();
  75. //}
  76. }
  77. lblend.Text = errorinfo;
  78. }
  79. catch (Exception ex)
  80. {
  81. LogBLL.Err(ex.Message);
  82. lblend.Text = errorinfo;
  83. }
  84. }
  85. private Socket ConnServer( ref string errorinfo)
  86. {
  87. try
  88. {
  89. IPAddress ip = IPAddress.Parse(txtip.Text);//服务端所在IP
  90. //IPEndPoint ipEnd = new IPEndPoint(ip, 5888);//服务端所监听的接口
  91. IPEndPoint ipEnd = new IPEndPoint(ip,Convert.ToInt32(txtport.Text));//服务端所监听的接口
  92. Socket CurrSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//初始化一个Socket对象
  93. CurrSocket.Connect(ipEnd);
  94. //此语句只支持windows系统,不支持linux系统
  95. //CurrSocket.IOControl(IOControlCode.KeepAliveValues, GetKeepAliveData(), null);
  96. ////处理来自机台的包,设置接受超时时间
  97. CurrSocket.ReceiveTimeout = 1000*5;
  98. CurrSocket.SendTimeout = 1000 * 5;
  99. errorinfo = "连接成功!!!";
  100. return CurrSocket;
  101. }
  102. catch (Exception ex)
  103. {
  104. errorinfo = ex.Message.ToString();
  105. return null;
  106. }
  107. }
  108. private void btnclose_Click(object sender, EventArgs e)
  109. {
  110. if (filesocket != null)
  111. {
  112. filesocket.Close();
  113. lblend.Text = "关闭成功~";
  114. }
  115. }
  116. private byte[] GetFileDatas(Int32 mode, Int32 result, string msg)
  117. {
  118. byte[] msgbytes = Encoding.UTF8.GetBytes(msg);
  119. Int32 len = msgbytes.Length + 12;
  120. byte[] alldatas = BitConverter.GetBytes(len);
  121. byte[] forAll = (alldatas.Concat(BitConverter.GetBytes(mode)).Concat(BitConverter.GetBytes(result)).Concat(msgbytes)).ToArray();
  122. return forAll;
  123. }
  124. private byte[] ReadData(Socket CurrSocket, ref int alllen, ref string errorinfo)
  125. {
  126. byte[] allbuffs = null;
  127. try
  128. {
  129. byte[] tempbuff = new byte[4];
  130. byte[] bytesbuff = new byte[4];
  131. int result = 0;
  132. int levlen = 4;
  133. int zerocount = 0;
  134. while (levlen > 0)
  135. {
  136. //说明前面4位还没接受完,需要继续接受
  137. result = CurrSocket.Receive(tempbuff, levlen, SocketFlags.None);//先读取4位块长度字节
  138. if (result == 0 && zerocount <= 20)
  139. {
  140. zerocount++;
  141. //如果返回数据长度为0,则休眠1秒钟,然后继续读取
  142. Thread.Sleep(1000);
  143. continue;
  144. }
  145. if (result <= 0)
  146. {
  147. errorinfo = $"未能从待机程序监听上读取数据,可能机台已经断开了,接受长度={result} ";
  148. return null;
  149. }
  150. if (result < levlen)
  151. {
  152. //添加测试代码
  153. //int a = 10;
  154. }
  155. if (result > 0)
  156. {
  157. Array.Copy(tempbuff, 0, bytesbuff, 4 - levlen, result);
  158. }
  159. levlen = levlen - result;
  160. }
  161. //Array.Reverse(bytesbuff);//20200920
  162. int len = BitConverter.ToInt32(bytesbuff, 0) - 4;
  163. alllen = len;
  164. if (len > 10)
  165. {
  166. int a = 10;
  167. }
  168. byte[] buffs = new byte[len];
  169. tempbuff = new byte[len];
  170. levlen = len;
  171. zerocount = 0;
  172. while (levlen > 0)
  173. {
  174. //说明还没接受完,需要继续接受
  175. result = CurrSocket.Receive(tempbuff, levlen, SocketFlags.None);
  176. if (result == 0 && zerocount <= 20)
  177. {
  178. zerocount++;
  179. //如果返回数据长度为0,则休眠1秒钟,然后继续读取
  180. Thread.Sleep(1000);
  181. continue;
  182. }
  183. if (result <= 0)
  184. {
  185. errorinfo = $"未能从待机程序监听上读取数据,可能机台已经断开了,接受长度={result} ";
  186. return null;
  187. }
  188. if (result > 0)
  189. {
  190. Array.Copy(tempbuff, 0, buffs, len - levlen, result);
  191. }
  192. levlen = levlen - result;
  193. }
  194. //result =mysocket.Receive(buffs,len,SocketFlags.None);
  195. //Array.Reverse(bytesbuff);//20200920
  196. allbuffs = new byte[len + 4];
  197. Array.Copy(bytesbuff, allbuffs, 4);
  198. Array.Copy(buffs, 0, allbuffs, 4, len);
  199. alllen = len + 4;
  200. return allbuffs;
  201. }
  202. catch (SocketException ex)
  203. {
  204. errorinfo = "待机程序Socket接受数据发生异常,错误信息为:" + ex.SocketErrorCode.ToString();
  205. return null;
  206. }
  207. catch (Exception ex)
  208. {
  209. errorinfo = ex.Message.ToString();
  210. errorinfo = "待机程序Socket接受数据发生异常,错误信息为:" + errorinfo;
  211. return null;
  212. }
  213. }
  214. }
  215. }