void ReceiveMsg(object o) { Socket client = o as Socket; while (true) { try { byte[] buffer = new byte[1024 * 1024]; int n = client.Receive(buffer); volumecontrol t = new volumecontrol(); int size = Marshal.SizeOf(typeof(volumecontrol)); IntPtr testi = Marshal.AllocHGlobal(n); Marshal.Copy(buffer, 0, testi, size); t = (volumecontrol)Marshal.PtrToStructure(testi, typeof(volumecontrol)); buffer = new byte[1024 * 1024]; n = client.Receive(buffer); t.carName = Encoding.UTF8.GetString(buffer, 0, n); buffer = new byte[1024 * 1024]; n = client.Receive(buffer); t.carSN = Encoding.UTF8.GetString(buffer, 0, n); //ShowMsg(client.RemoteEndPoint.ToString() + ":" + words); } catch (Exception ex) { NewMain.Nform.logClass.write(ex.Message); break; } } }
public bool sendpackage(int flag, volumecontrol rhs) { try { byte[] buf = Encoding.UTF8.GetBytes(Convert.ToString(flag)); client.Send(buf); Thread.Sleep(10); volumepackage t = new volumepackage(); t.carVolume = Convert.ToDouble(rhs.carVolume); t.loadingrat = rhs.Loadingrate; t.volume = Convert.ToDouble(rhs.Volume); BinaryFormatter bf; bf = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); bf.Serialize(stream, t); byte[] buff = stream.ToArray(); client.Send(buff, buff.Length, 0); //int size = Marshal.SizeOf(typeof(volumecontrol)); //得到结构体大小 //IntPtr buffer = Marshal.AllocHGlobal(size); //开辟内存空间 //Marshal.StructureToPtr(rhs, buffer, false); //填充内存空间 //byte[] bytes = new byte[size]; //Marshal.Copy(buffer, bytes, 0, size); //填充数组 //client.Send(bytes); } catch (Exception ex) { NewMain.Nform.logClass.write(ex.Message); } return(true); }
public void UpdateVolume(volumecontrol v) { data.volume = v; if (SendMessage(Messagetype.volumepackage)) { Console.WriteLine("发送成功给用户!"); } else { Console.WriteLine("error!"); } }
public void sendmessage(int flag, volumecontrol volumeCalc) { try { byte[] buf = Encoding.UTF8.GetBytes(Convert.ToString(flag)); client.Send(buf); byte[] buf2 = Encoding.UTF8.GetBytes(volumeCalc.carName + " " + volumeCalc.carSN + " " + Convert.ToString(NewMain.Nform.checkBox1.Enabled)); servermessageflag = -1; client.Send(buf2); } catch (Exception ex) { NewMain.Nform.logClass.write(ex.Message); MessageBox.Show(ex.Message); } }
/// <summary> /// 空构造函数 /// </summary> public DeviceData() : base() { configtime = new configtimexml[3]; volume = new volumecontrol(); }
/// <summary> /// 构造函数 /// </summary> /// <param name="typedata">数据类型</param> /// <param name="typesystem">所属系统</param> public DeviceData(string typedata, string typesystem) : base(typedata, typesystem) { configtime = new configtimexml[3]; volume = new volumecontrol(); }
public TypeData() { datatype = new Datatype(); configtime = new configtimexml[3]; volume = new volumecontrol(); }
public void updatemessage(volumecontrol newvolume, configtimexml[] newconfig) { data.volume.carName = newvolume.carName; data.configtime = newconfig; data.volume.carVolume = newvolume.carVolume; SendUpdate(); }