public bool SendData(byte[] data, out string errMsg) { errMsg = ""; if (lstClient.Count == 1) { try { UDPClient client = lstClient.ToArray()[0]; client.NetWork.Send(data, data.Length); } catch (Exception ex) { errMsg = ex.Message; return(false); } } else { errMsg = "客户端列表不唯一"; return(false); } return(true); }
/// <summary> /// 接收到数据 /// </summary> /// <param name="ar"></param> public void ReceiveCallback(IAsyncResult ar) { UDPClient uclient = (UDPClient)ar.AsyncState; try { if (uclient.NetWork.Client != null && uclient.NetWork.Client.Connected) { IPEndPoint fclient = uclient.ipLocalEndPoint; Byte[] recdata = uclient.NetWork.EndReceive(ar, ref fclient); string ConnName = uclient.ipLocalEndPoint.Port + "->" + fclient.ToString(); if (DataReceived != null) { DataReceived.BeginInvoke(ConnName, recdata, null, null); //异步输出数据 } uclient.NetWork.BeginReceive(new AsyncCallback(ReceiveCallback), uclient); //继续异步接收数据 } } catch (Exception ex) { TriggerErrorEvent(ex.Message); // MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public TestHome(UDPClient client, UDPClientManager clienManager) { this.client = client; this.clienManager = clienManager; InitializeComponent(); }