UdpMassage.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Threading;
  3. using ToolsClassLibrary;
  4. namespace IntelligentControlForsx.Code
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public static class UdpMassage
  10. {
  11. static UdpHelper udp = new UdpHelper();
  12. static Thread threadServer;
  13. public static void UdpSocketStart()
  14. {
  15. udp.ClientBind();
  16. threadServer = new Thread(recMsg);
  17. threadServer.IsBackground = true;
  18. threadServer.Start();
  19. }
  20. static void recMsg()
  21. {
  22. while (true)
  23. {
  24. byte[] data;
  25. try
  26. {
  27. data = udp.Receive();
  28. }
  29. catch (Exception ex)
  30. {
  31. Console.WriteLine(string.Format("出现异常:{0}", ex.Message));
  32. continue;
  33. }
  34. object dic = JsonHelper.DeSerializeBinary(data);
  35. SocketData<double> udpdata = (SocketData<double>)dic;
  36. foreach (var key in udpdata.FileData.Keys)
  37. {
  38. string sid = key;
  39. double sval =udpdata.FileData[key];
  40. CommunalTarget.UdpDoubleMemory.AddorUpdate(sid, sval);
  41. }
  42. }
  43. }
  44. }
  45. }