c# udp通讯实现
public class launcherJson { public string dispatcher_ip; // dispatcher地址 public int dispatcher_port; // dispatcher端口 5999 public string launcher_ip; // launcher地址 public int launcher_port; // launcher端口 6001 public string gtaplugin_ip; // gtaplugin地址 public int gtaplugin_port; // gtaplugin端口 6000 public string gta_game_depth_path; public string gta_game_truth_path; public string gta_game_config_path; public string renderdoc_exec_path; public string capture_data_dir; public string task_json_path; public int time_wait_before_capture; // 2分钟 2*60*1000 } public class Comm { UdpClient udp_recv = null; System.Timers.Timer timer = null; private launcherJson launcher_json = null; public void init(ref launcherJson launcher, string file_path) { string json = File.ReadAllText(file_path, Encoding.UTF8); if (json.Length > 0) { launcher = JsonConvert.DeserializeObject<launcherJson>(json); } } public Comm(launcherJson json) { launcher_json = json; udp_recv = new UdpClient(new IPEndPoint(IPAddress.Parse(json.launcher_ip), json.launcher_port));//端口要与发送端相同 Thread thread = new Thread(recv_msg);//用线程接收,避免UI卡住 thread.IsBackground = true; thread.Start(); timer = new System.Timers.Timer(); timer.Interval = json.time_wait_before_capture; timer.Elapsed += delegate { string run_task_msg = string.Format("run_task,{0}", json.task_json_path); send_msg(json.gtaplugin_ip, json.gtaplugin_port, run_task_msg); }; timer.Start(); } public void require_task() { send_msg(launcher_json.dispatcher_ip, launcher_json.dispatcher_port, "请发采集任务"); } public void send_msg(string ip, int port, string msg) { IPEndPoint ip_pt = new IPEndPoint(IPAddress.Parse(ip), port); byte[] data = Encoding.UTF8.GetBytes(msg); udp_recv.Send(data, data.Length, ip_pt); } public void recv_msg(object obj) { IPEndPoint recv_ip = new IPEndPoint(IPAddress.Any, 0); while (true) { try { byte[] bytRecv = udp_recv.Receive(ref recv_ip); string message = Encoding.UTF8.GetString(bytRecv, 0, bytRecv.Length); Console.WriteLine("recv:" + message); Thread.Sleep(100); if(recv_ip.Address== IPAddress.Parse(launcher_json.dispatcher_ip)) { // write json File.WriteAllText(launcher_json.task_json_path, message); } else if (recv_ip.Address == IPAddress.Parse(launcher_json.gtaplugin_ip)) { // handle } } catch (Exception ex) { Console.WriteLine("Exception:" + ex.Message); //break; } } } public void modify_screen_solution(string xml_file, taskJson task) { } public void modify_task_solution() { } public void start_process_renderdoc(string exe_file, string launcher_cfg) { if (File.Exists(exe_file) && File.Exists(launcher_cfg)) { FileInfo exe = new FileInfo(exe_file); FileInfo cfg = new FileInfo(launcher_cfg); //copy config files string new_cfg = Path.Combine(exe.DirectoryName, cfg.Name); File.Copy(launcher_cfg, new_cfg, true); ProcessStartInfo startinfo = new ProcessStartInfo(exe_file); startinfo.WorkingDirectory = exe.DirectoryName; Process process = new Process(); process.StartInfo = startinfo; process.Start(); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人