c# 使用UdpClient发送接收数据

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
 
namespace UDP_DEMO_Send {
    static class Program {
        static void Main(string[] args) {
 
            InitUdp();
 
            Console.ReadKey();
        }
 
        #region UDP通信
 
        private static byte[] udpResult = new byte[1024];
        private static IPEndPoint udpTargetSend = null;
        private static UdpClient udpSend = null;
 
        private static int InitUdp() {
 
            var ip = "192.168.0.92";
            var port = 5021;
 
            Log(LogType.Open, "UDP Servise IP." + ip);
            Log(LogType.Open, "UDP Servise Port." + port);
 
            try {
                udpTargetSend = new IPEndPoint(IPAddress.Parse(ip), port);
 
                udpSend = new UdpClient(5011);
 
                while (true) {
 
                    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);
                    if (r != 0) {
                        Log(LogType.Recv, "rs err " + r);
                        Thread.Sleep(1000);
                        continue;
                    }
                    Log(LogType.Recv, BitConverter.ToString(udpResult, 0, udpResult.Length));
                    Thread.Sleep(1000);
                }
 
            } catch (Exception e) {
                Log(LogType.Error, e.Message);
            }
 
            return 0;
 
        }
 
        static long send_expire = 0;
        static int send(byte[] buf, int ti = 3000) {
 
            udpResult = new byte[1024];
            Log(LogType.Send, BitConverter.ToString(buf));
 
            var r = 0;
 
            try {
                r = udpSend.Send(buf, buf.Length, udpTargetSend);
            } catch (Exception ex) {
                Log(LogType.Error, ex.Message);
            }
 
            send_expire = DateTime.Now.Ticks + ti * 10000;
            int len = 7;//最短数据长度位置
 
            for (; DateTime.Now.Ticks < send_expire; Thread.Sleep(20)) {
                IPEndPoint udpTargetRecv = null;
                try {
                    if (udpSend.Available <= 0) continue;
                    udpResult = udpSend.Receive(ref udpTargetRecv);
 
                    if (udpResult.Length >= len) break;
 
                } catch (Exception e) {
                    Log(LogType.Error, e.Message);
                    return 4;
                }
            }
 
            if (udpResult == null || udpResult.Length <= 0) {
                Log(LogType.Recv, "time out");
                return 4;
            }
 
            return 0;
        }
 
        #endregion
 
        private enum LogType {
            Open,
            Send,
            Recv,
            Error,
            Info,
        }
 
        private static void Log(LogType t, string msg) {
 
            Console.Write($"{DateTime.Now:yyyy-MM-dd HH:mm:ss:fff} [{t}] { msg}{Environment.NewLine}");
 
        }
    }
 
    public static class Expand {
 
        public static string Replace(this string s, string p, string r) {
            return Regex.Replace(s, p, r);
        }
 
        public static byte[] ToBytes(this string s) {
            s = Replace(s, @"[^\dA-Fa-f]+", "");
            if (s.Length % 2 > 0) s = "0" + s;
            var max = s.Length / 2;
            var buf = new byte[max];
            for (var i = 0; i < max; i++) buf[i] = Convert.ToByte(s.Substring(i * 2, 2), 16);
            return buf;
        }
 
    }
}

相关推荐

  1. vue如何使用websocket去接收数据发送数据

    2024-06-19 09:36:07       20 阅读
  2. python使用rabbitmq发送消息和接收消息数据

    2024-06-19 09:36:07       27 阅读
  3. WebSocket发送接收数据

    2024-06-19 09:36:07       20 阅读
  4. udp进行数据发送接收

    2024-06-19 09:36:07       39 阅读
  5. 本地模拟发送接收RabbitMQ数据

    2024-06-19 09:36:07       38 阅读

最近更新

  1. Redis哨兵模式实践

    2024-06-19 09:36:07       0 阅读
  2. MLlib机器学习入门:用Spark打造预测模型

    2024-06-19 09:36:07       0 阅读
  3. python绘制函数调用图总结

    2024-06-19 09:36:07       0 阅读
  4. PHP 表单验证:邮件和URL

    2024-06-19 09:36:07       0 阅读
  5. org.apache.ibatis.session是什么?

    2024-06-19 09:36:07       0 阅读
  6. C语言 指针方法 有一个班4个学生,5门课程

    2024-06-19 09:36:07       0 阅读
  7. C++中的explicit关键字

    2024-06-19 09:36:07       0 阅读

热门阅读

  1. MongoDB基础知识

    2024-06-19 09:36:07       14 阅读
  2. 十三、数论基础

    2024-06-19 09:36:07       13 阅读
  3. Ruby 数据库访问 - DBI 教程

    2024-06-19 09:36:07       17 阅读
  4. 安卓交叉编译——ndk

    2024-06-19 09:36:07       16 阅读
  5. Swarm 集群管理

    2024-06-19 09:36:07       14 阅读
  6. PostgreSQL源码分析——创建用户

    2024-06-19 09:36:07       14 阅读
  7. Linux 上的 TTY 是什么?

    2024-06-19 09:36:07       16 阅读
  8. USB 端点停止

    2024-06-19 09:36:07       14 阅读