ScanSocket.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. namespace DllScan
  8. {
  9. class ScanSocket
  10. {
  11. private Socket CurrSocket = null;
  12. //private Machine CurrMac = null;
  13. //public Hsms(Machine mac)
  14. //{
  15. //}
  16. //与设备建立连接
  17. //public bool InitSocket(Machine mac,ref string errorinfo)
  18. //{
  19. // try
  20. // {
  21. // CurrMac = mac;
  22. // if (!ShutDownSocket(ref errorinfo))
  23. // {
  24. // errorinfo = "关闭socket错误:" + errorinfo;
  25. // return false;
  26. // }
  27. // IPAddress ip = IPAddress.Parse(CurrMac.IPAddress);//服务端所在IP
  28. // IPEndPoint ipEnd = new IPEndPoint(ip, CurrMac.FPort);//服务端所监听的接口
  29. // CurrSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//初始化一个Socket对象
  30. // CurrSocket.Connect(ipEnd);
  31. // ////处理来自机台的包,设置接受超时时间为60秒
  32. // //建立握手,产生握手二进制数组
  33. // byte[] bytes = HsmsUnity.HexStrTobyte(HsmsUnity.SelectReq);
  34. // int result = SendMsg(bytes, ref errorinfo);
  35. // if (result <= 0)
  36. // {
  37. // errorinfo = "发送数据1错误:" + errorinfo;
  38. // return false;
  39. // }
  40. // bytes = ReadData(ref errorinfo);
  41. // if (bytes == null)
  42. // {
  43. // errorinfo = "接受数据1错误:" + errorinfo;
  44. // return false;
  45. // }
  46. // string temphexstr = HsmsUnity.byteToHexStr(bytes, " ");
  47. // if (temphexstr != HsmsUnity.SelectRsq)//说明没有接受到正确的响应包,则初始化不成功
  48. // {
  49. // errorinfo = "建立连接时,没有收到正确的响应包!";
  50. // return false;
  51. // }
  52. // return true;
  53. // }
  54. // catch (Exception ex)
  55. // {
  56. // errorinfo = ex.Message.ToString();
  57. // return false;
  58. // }
  59. //}
  60. public bool InitSocket(string ipaddress, int fport, ref string errorinfo)
  61. {
  62. try
  63. {
  64. if (!ShutDownSocket(ref errorinfo))
  65. {
  66. errorinfo = "关闭socket错误:" + errorinfo;
  67. return false;
  68. }
  69. IPAddress ip = IPAddress.Parse(ipaddress);//服务端所在IP
  70. //IPEndPoint ipEnd = new IPEndPoint(ip, fport);//服务端所监听的接口
  71. IPEndPoint ipEnd = new IPEndPoint(ip, 5999);//服务端所监听的接口
  72. CurrSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//初始化一个Socket对象
  73. CurrSocket.Connect(ipEnd);
  74. uint dummy = 0;
  75. byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3];
  76. BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);//启用Keep-Alive
  77. BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy));//在这个时间间隔内没有数据交互,则发探测包 毫秒
  78. BitConverter.GetBytes((uint)1000).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2);//发探测包时间间隔 毫秒
  79. CurrSocket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
  80. CurrSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
  81. ////处理来自机台的包,设置接受超时时间为60秒
  82. //CurrSocket.ReceiveTimeout = 1000 * 60;
  83. //CurrSocket.SendTimeout = 1000 * 60;
  84. return true;
  85. }
  86. catch (Exception ex)
  87. {
  88. errorinfo = ex.Message.ToString();
  89. return false;
  90. }
  91. }
  92. //关闭连接
  93. public bool ShutDownSocket(ref string errorinfo)
  94. {
  95. try
  96. {
  97. if (CurrSocket == null)
  98. return true;
  99. //if (CurrSocket.Connected)
  100. //{
  101. //}
  102. //CurrSocket.Shutdown(SocketShutdown.Both);
  103. CurrSocket.Close();//关闭Socket
  104. CurrSocket = null;
  105. return true;
  106. }
  107. catch (Exception ex)
  108. {
  109. CurrSocket = null;
  110. errorinfo = ex.Message.ToString();
  111. return false;
  112. }
  113. }
  114. //发送数据
  115. private int SendMsg(byte[] datas, ref string errorinfo)
  116. {
  117. try
  118. {
  119. int result = CurrSocket.Send(datas);
  120. return result;
  121. }
  122. catch (Exception ex)
  123. {
  124. errorinfo = ex.Message.ToString();
  125. errorinfo = "Socket发送数据发生异常,错误信息为:" + errorinfo;
  126. return -1;
  127. }
  128. }
  129. //socket读取数据
  130. private byte[] ReadData(ref string errorinfo)
  131. {
  132. try
  133. {
  134. //uint dummy = 0;
  135. //byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3];
  136. //BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);//启用Keep-Alive
  137. //BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy));//在这个时间间隔内没有数据交互,则发探测包 毫秒
  138. //BitConverter.GetBytes((uint)1000).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2);//发探测包时间间隔 毫秒
  139. //CurrSocket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
  140. //CurrSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
  141. byte[] allbuffs = new byte[100];
  142. int result = CurrSocket.Receive(allbuffs, 100, SocketFlags.None);
  143. if (result <= 0)
  144. {
  145. errorinfo = "未能从机台上读取数据,可能机台已经断开了。";
  146. return null;
  147. }
  148. byte[] tempbuff = new byte[result];
  149. Array.Copy(allbuffs, 0, tempbuff, 0, result);
  150. return tempbuff;
  151. }
  152. catch (SocketException ex)
  153. {
  154. errorinfo = "Socket接受数据发生异常,错误信息为:" + ex.SocketErrorCode.ToString();
  155. return null;
  156. }
  157. catch (Exception ex)
  158. {
  159. errorinfo = ex.Message.ToString();
  160. errorinfo = "Socket接受数据发生异常,错误信息为:" + errorinfo;
  161. return null;
  162. }
  163. }
  164. private byte[] GetKeepAliveData()
  165. {
  166. uint dummy = 0;
  167. byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3];
  168. BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);
  169. BitConverter.GetBytes((uint)3000).CopyTo(inOptionValues, Marshal.SizeOf(dummy));//keep-alive间隔
  170. BitConverter.GetBytes((uint)500).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2);// 尝试间隔
  171. return inOptionValues;
  172. }
  173. /// <summary>
  174. /// 接受设备指令
  175. /// </summary>
  176. /// <param name="order"></param>
  177. /// <param name="datas"></param>
  178. /// <returns></returns>
  179. public byte[] ReceiveForBytes(ref string errorinfo)
  180. {
  181. try
  182. {
  183. byte[] receivedatas = ReadData(ref errorinfo);
  184. return receivedatas;
  185. }
  186. catch (Exception ex)
  187. {
  188. errorinfo = ex.Message.ToString();
  189. return null;
  190. }
  191. }
  192. public ScanBlock ReceiveForBlock(ref string errorinfo)
  193. {
  194. try
  195. {
  196. byte[] receivedatas = ReadData(ref errorinfo);
  197. if (receivedatas == null)
  198. return null;
  199. string scanstr= System.Text.Encoding.ASCII.GetString(receivedatas);
  200. ScanBlock receiveblock = new ScanBlock(scanstr,"",DateTime.Now);
  201. return receiveblock;
  202. }
  203. catch (Exception ex)
  204. {
  205. errorinfo = ex.Message.ToString();
  206. return null;
  207. }
  208. }
  209. public int SendForBytes(byte[] datas, ref string errorinfo)
  210. {
  211. try
  212. {
  213. int result = SendMsg(datas, ref errorinfo);
  214. return result;
  215. }
  216. catch (Exception ex)
  217. {
  218. errorinfo = ex.Message.ToString();
  219. return -1;
  220. }
  221. }
  222. public int SetReceiveTimeout(int timeout)
  223. {
  224. CurrSocket.ReceiveTimeout = timeout;
  225. return 1;
  226. }
  227. public int SetSendTimeout(int timeout)
  228. {
  229. CurrSocket.SendTimeout = timeout;
  230. return 1;
  231. }
  232. }
  233. }