protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); IoC = new Container(); Logger.EnableDebugLogger(); var config = UdpPack.DeserializeFromFile(AppDomain.CurrentDomain.BaseDirectory + "AltPack.json"); IoC.AddOrUpdate(new AltPackViewModel(config, 2233)); // 端口号与接收器一致 }
private static void JsonPackConfig_Check() { try { UdpPack pack = UdpPack.DeserializeFromFile("AltPack.json"); Console.WriteLine("Desializing..."); Console.WriteLine(pack.ToString()); Console.WriteLine("Serializing..."); Console.WriteLine(pack.SerializeToJsonString()); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public AltPackViewModel(UdpPack config) { Gps = new AltViewModel { Name = config.Items[0].Name, Unit = config.Items[0].Unit, Minimum = config.Items[0].Minimum, Maximum = config.Items[0].Maximum }; Baro = new AltViewModel { Name = config.Items[1].Name, Unit = config.Items[1].Unit, Minimum = config.Items[1].Minimum, Maximum = config.Items[1].Maximum }; PackName = config.PackName; PackId = config.PackID; }
/// <summary> /// 会启动Udp发送器的实例,默认采用组播模式,只需设置端口号一致即可接收到 /// </summary> /// <param name="config">json配置实例</param> /// <param name="port">端口号</param> public AltPackViewModel(UdpPack config, int port) : this(config) { Gps.PropertyChanged += OnPropertyChanged; Sender = new UdpSender(port, true); }
//连接事件 private void btnBin_Click(object sender, EventArgs e) { try { Iep = new IPEndPoint(IPAddress.Parse(txtLocalIP.Text), int.Parse(txtk1.Text)); client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); client.Bind(Iep); rtbMsgList.AppendText("成功运行......\n"); ListUdp = new List <UdpPack>(); if (!string.IsNullOrEmpty(txtLocalIP.Text)) { new Task(() => { #region 循环接收数据 while (true) { try { EndPoint remote = new IPEndPoint(IPAddress.Any, 0); byte[] by = new byte[20480]; int recv = client.ReceiveFrom(by, ref remote); byte[] ort = new byte[recv]; Buffer.BlockCopy(by, 1, ort, 0, recv); //判断接收的字节数组是字符串还是文件 if (by[0] == 0) { #region 接收字符串 string name = string.Format("{1}", remote, Encoding.UTF8.GetString(ort, 0, recv)); Action action = new Action(() => { rtbMsgList.AppendText(name + "\n"); frmPopups po = new frmPopups(name); po.ShowDialog(); }); this.Invoke(action); #endregion } else { #region 接收文件保存 object obj = new object(); byte[] dt = new byte[by.Length - 1]; Buffer.BlockCopy(by, 1, dt, 0, by.Length - 1); MemoryStream ms = new MemoryStream(dt); ms.Position = 0; BinaryFormatter formatter = new BinaryFormatter(); obj = formatter.Deserialize(ms);//把内存流反序列成对象 ms.Close(); UdpPack pa = (UdpPack)obj; count = pa.count; bl = pa.bl; Names = Encoding.UTF8.GetString(pa.packname, 0, pa.packname.Length); bool bol = true; if (ListUdp.Count == 0) { ListUdp.Add(pa); } else { foreach (var item in ListUdp) { if (pa.id == item.id) { bol = false; } } if (bol) { ListUdp.Add(pa); } } if (count != 0) { if (ListUdp.Count >= count) { if (MessageBox.Show("是否接收文件 " + Names, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { Thread InvokeThread = new Thread(new ThreadStart(Preservation)); InvokeThread.SetApartmentState(ApartmentState.STA); InvokeThread.Start(); InvokeThread.Join(); } } } #endregion } } catch (Exception ex) { Action action = new Action(() => { rtbMsgList.AppendText(ex.Message); }); this.Invoke(action); break; } } #endregion }).Start(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void txtMsg_DragDrop(object sender, DragEventArgs e) { //sender.GetType().Name; string name = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); try { string[] strName = name.Split('\\'); string strPack = strName[strName.Count() - 1]; byte[] packName = Encoding.UTF8.GetBytes(strPack); FileStream fs = new FileStream(name, FileMode.Open, FileAccess.Read); byte[] pack = new byte[fs.Length]; fs.Read(pack, 0, (int)fs.Length); long fd = 6144; List <UdpPack> ListUdp = new List <UdpPack>(); UdpPack udpPack = null; int count = pack.Length / 6144; if (pack.Length < 6144) { count = 1; } byte[] ps = null; for (int i = 0; i < count; i++) { udpPack = new UdpPack(); //19619565/61440 ps = new byte[fd]; if (pack.Length - (fd * i) > fd) { Array.Copy(pack, i * 6144, ps, 0, fd); } else { Array.Copy(pack, i * 6144, ps, 0, pack.Length - (fd * i)); } udpPack.id = i + 1; udpPack.count = count; udpPack.pack = ps; udpPack.bl = pack.Length; udpPack.packname = packName; ListUdp.Add(udpPack); } int position = 0; foreach (var item in ListUdp) { MemoryStream ms = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, (object)item); ms.Position = 0; byte[] bytes = ms.GetBuffer(); ms.Read(bytes, 0, bytes.Length); ms.Close(); IPEndPoint remote = new IPEndPoint(IPAddress.Parse(txtProcessIP.Text), int.Parse(txtk2.Text)); byte[] bytets = new byte[bytes.Length + 1]; bytets[0] = 1; Buffer.BlockCopy(bytes, 0, bytets, 1, bytes.Length); //client.SendTo(bytets, remote); client.BeginSendTo(bytets, 0, bytets.Length, SocketFlags.None, remote, async => { int send = client.EndSendTo(async); }, null); Thread.Sleep(100); position++; ShowProgressSchedule(ListUdp.Count, position); //label2.Text = "文件发送成功!"; InvokeMsg("文件发送成功"); } } catch (Exception) { throw; } }