• 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. }

  • 相关阅读:
    MongoDB初识(一)
    python的自定义函数的用法和实例
    Java装饰者模式
    .NET开源、跨平台的本地日记APP - SwashbucklerDiary
    微服务中的相关概念
    AQS是什么?AbstractQueuedSynchronizer之AQS原理及源码深度分析
    友元 C++进修之路系列
    单片机学到什么程度才可以去工作?
    10个实用的JS小技巧分享
    C# WebSocket 服务器
  • 原文地址:https://blog.csdn.net/Pei_hua100/article/details/139764818