public bool Send(string str, IPAddress Remoteendpont) { bool boo = false; try { UDPMsgEntity entity = Sysconstant.LocalBordcastEntity; entity.MsgType = 1; entity.Msg = str; var bytes = SerializeHelper.Serialize(entity); //var bytes = System.Text.Encoding.UTF8.GetBytes("msg:" + str); if (Remoteendpont != null) { //System.Net.IPEndPoint endpoint = Remoteendpont; client.Send(bytes, bytes.Length, new IPEndPoint(Remoteendpont, Sysconstant.port));// endpoint); boo = true; } else { //System.Windows.Forms.MessageBox.Show("没有可发送对象"); } } catch (Exception ex) { //System.Windows.Forms.MessageBox.Show(ex.ToString()); if (ErrorEvent != null) { ErrorEvent(null, ex.ToString()); } } return(boo); }
private void SendBordcast(object o) { try { UDPMsgEntity entity = Sysconstant.LocalBordcastEntity; var bytes = SerializeHelper.Serialize(entity); if (!Sysconstant.MutiBordcast) { System.Net.IPEndPoint endpoint = new System.Net.IPEndPoint(System.Net.IPAddress.Broadcast, port); var count = client.Send(bytes, bytes.Length, endpoint); } else { foreach (IPAddress ip in Sysconstant.LocalIPAddress) { int index = ip.ToString().LastIndexOf("."); string ip1 = ip.ToString().Substring(0, index); string ip2 = ip1 + ".255"; IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(ip2), port); var count = client.Send(bytes, bytes.Length, endpoint); } } } catch (Exception ex) { //System.Windows.Forms.MessageBox.Show(ex.ToString()); //if (ErrorEvent != null) //{ // ErrorEvent(null, ex.ToString()); //} log.Log.WriteLog(ex.ToString()); } }