当前位置:   article > 正文

c# 使用UdpClient发送接收数据

c# 使用UdpClient发送接收数据

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading;
  10. namespace UDP_DEMO_Send {
  11. static class Program {
  12. static void Main(string[] args) {
  13. InitUdp();
  14. Console.ReadKey();
  15. }
  16. #region UDP通信
  17. private static byte[] udpResult = new byte[1024];
  18. private static IPEndPoint udpTargetSend = null;
  19. private static UdpClient udpSend = null;
  20. private static int InitUdp() {
  21. var ip = "192.168.0.92";
  22. var port = 5021;
  23. Log(LogType.Open, "UDP Servise IP." + ip);
  24. Log(LogType.Open, "UDP Servise Port." + port);
  25. try {
  26. udpTargetSend = new IPEndPoint(IPAddress.Parse(ip), port);
  27. udpSend = new UdpClient(5011);
  28. while (true) {
  29. var r = send(("5a3c 12 01 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-01-DB").ToBytes(), 500);
  30. if (r != 0) {
  31. Log(LogType.Recv, "rs err " + r);
  32. Thread.Sleep(1000);
  33. continue;
  34. }
  35. Log(LogType.Recv, BitConverter.ToString(udpResult, 0, udpResult.Length));
  36. Thread.Sleep(1000);
  37. }
  38. } catch (Exception e) {
  39. Log(LogType.Error, e.Message);
  40. }
  41. return 0;
  42. }
  43. static long send_expire = 0;
  44. static int send(byte[] buf, int ti = 3000) {
  45. udpResult = new byte[1024];
  46. Log(LogType.Send, BitConverter.ToString(buf));
  47. var r = 0;
  48. try {
  49. r = udpSend.Send(buf, buf.Length, udpTargetSend);
  50. } catch (Exception ex) {
  51. Log(LogType.Error, ex.Message);
  52. }
  53. send_expire = DateTime.Now.Ticks + ti * 10000;
  54. int len = 7;//最短数据长度位置
  55. for (; DateTime.Now.Ticks < send_expire; Thread.Sleep(20)) {
  56. IPEndPoint udpTargetRecv = null;
  57. try {
  58. if (udpSend.Available <= 0) continue;
  59. udpResult = udpSend.Receive(ref udpTargetRecv);
  60. if (udpResult.Length >= len) break;
  61. } catch (Exception e) {
  62. Log(LogType.Error, e.Message);
  63. return 4;
  64. }
  65. }
  66. if (udpResult == null || udpResult.Length <= 0) {
  67. Log(LogType.Recv, "time out");
  68. return 4;
  69. }
  70. return 0;
  71. }
  72. #endregion
  73. private enum LogType {
  74. Open,
  75. Send,
  76. Recv,
  77. Error,
  78. Info,
  79. }
  80. private static void Log(LogType t, string msg) {
  81. Console.Write($"{DateTime.Now:yyyy-MM-dd HH:mm:ss:fff} [{t}] { msg}{Environment.NewLine}");
  82. }
  83. }
  84. public static class Expand {
  85. public static string Replace(this string s, string p, string r) {
  86. return Regex.Replace(s, p, r);
  87. }
  88. public static byte[] ToBytes(this string s) {
  89. s = Replace(s, @"[^\dA-Fa-f]+", "");
  90. if (s.Length % 2 > 0) s = "0" + s;
  91. var max = s.Length / 2;
  92. var buf = new byte[max];
  93. for (var i = 0; i < max; i++) buf[i] = Convert.ToByte(s.Substring(i * 2, 2), 16);
  94. return buf;
  95. }
  96. }
  97. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/735967?site
推荐阅读
相关标签
  

闽ICP备14008679号