using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using XdCxRhDW.Dto; namespace XdCxRhDW.Sender { public partial class Form1 : DevExpress.XtraEditors.XtraForm { public Form1() { InitializeComponent(); } CancellationTokenSource cts1; TcpClient client1; private async void btn1_Click(object sender, EventArgs e) { layoutControlItem1.Enabled = false; layoutControlItem7.Enabled = false; if (btn1.Text == "推送") { try { cts1 = new CancellationTokenSource(); btn1.Text = "停止"; var addrArr = txtAddr1.Text.Trim().Replace(":", ":").Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); string ip = addrArr[0]; int port = Convert.ToInt32(addrArr[1]); S: if (string.IsNullOrWhiteSpace(localPort1.Text)) client1 = new TcpClient(); else client1 = new TcpClient(new IPEndPoint(IPAddress.Any, Convert.ToInt32(localPort1.Text))); client1.NoDelay = true; client1.SendTimeout = 5000; client1.ReceiveTimeout = 5000; while (!cts1.IsCancellationRequested) { try { await client1.ConnectAsync(IPAddress.Parse(ip), port); break; } catch (Exception ex) { Log(ex); await Task.Delay(5000, cts1.Token); } } Log($"已成功连接到[{txtAddr1.Text}]"); var lines = File.ReadAllLines("Simulation_Data2023.dat"); while (!cts1.IsCancellationRequested) { try { int idx = 1; foreach (var line in lines) { if (cts1.IsCancellationRequested) break; if (string.IsNullOrWhiteSpace(line)) continue; var items = line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); SendDto dto = new SendDto() { SxDto = Convert.ToDouble(items[0]), XdDto = Convert.ToDouble(items[1]), MainYbDto = Convert.ToDouble(items[2]), AdjaYbDto = Convert.ToDouble(items[3]), CxRes = Convert.ToDouble(items[4]), MainX = Convert.ToDouble(items[7]), MainY = Convert.ToDouble(items[8]), MainZ = Convert.ToDouble(items[9]), AdjaX = Convert.ToDouble(items[10]), AdjaY = Convert.ToDouble(items[11]), AdjaZ = Convert.ToDouble(items[12]), }; var msg = Newtonsoft.Json.JsonConvert.SerializeObject(dto); var data = Encoding.UTF8.GetBytes(msg); var dataWithHeader = BitConverter.GetBytes(data.Length).Concat(data).ToArray(); await client1.GetStream().WriteAsync(dataWithHeader, 0, dataWithHeader.Length); Log($"已向[{txtAddr1.Text}]发送第{idx++}条仿真结果"); await Task.Delay(3000, cts1.Token); } } catch (InvalidOperationException) { goto S; } catch (Exception ex) { Log($"向[{txtAddr1.Text}]发送结果失败.{ex.Message}"); await Task.Delay(5000, cts1.Token); } } } catch (Exception ex) { Log(ex); } finally { btn1.Text = "推送"; layoutControlItem1.Enabled = true; layoutControlItem7.Enabled = true; } } else { cts1?.Cancel(); client1?.Dispose(); } } private void Log(string msg) { try { if (this.InvokeRequired) { this.Invoke(new Action(() => { if (listBoxControl1.ItemCount > 5000) listBoxControl1.Items.Clear(); listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{msg}"); listBoxControl1.SelectedIndex = 0; })); } else { if (listBoxControl1.ItemCount > 5000) listBoxControl1.Items.Clear(); listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{msg}"); listBoxControl1.SelectedIndex = 0; } } catch { } } private void Log(Exception ex) { try { if (this.InvokeRequired) { this.Invoke(new Action(() => { if (listBoxControl1.ItemCount > 5000) listBoxControl1.Items.Clear(); listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{ex.Message}"); listBoxControl1.SelectedIndex = 0; })); } else { if (listBoxControl1.ItemCount > 5000) listBoxControl1.Items.Clear(); listBoxControl1.Items.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}--{ex.Message}"); listBoxControl1.SelectedIndex = 0; } } catch { } } private void listBoxControl1_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { popupMenu1.ShowPopup(MousePosition); } } private void btnClear_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { listBoxControl1.Items.Clear(); } private void btnCopyAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { StringBuilder sb = new StringBuilder(); foreach (var item in listBoxControl1.Items) { sb.AppendLine(item.ToString()); } var data = sb.ToString(); if (string.IsNullOrWhiteSpace(data)) return; Clipboard.SetText(sb.ToString()); } } }