static void client_Received(object sender, UdpEventArgs e) { IPEndPoint ep = e.Remote as IPEndPoint; string tmpReceived = Encoding.Default.GetString(e.Received); Console.WriteLine(ep.Address.ToString() + ":" + ep.Port + "--> " + tmpReceived); }
///////////////////// ///UDP 변경 ///////////////////// void UdpDataReceivedProcess(object sender, UdpEventArgs e) { int size = e.bdata.Length; float[] fdata = new float[size / 4]; Buffer.BlockCopy(e.bdata, 0, fdata, 0, size); cq.Enqueue(fdata); }
static void server_Received(object sender, UdpEventArgs e) { IPEndPoint ep = e.Remote as IPEndPoint; string tmpReceived = Encoding.Default.GetString(e.Received); Console.WriteLine(ep.Address.ToString() + ":" + ep.Port + "--> " + tmpReceived); ///自动回复 server.Send(Encoding.Default.GetBytes("服务器已收到数据:'" + tmpReceived + "',来自:‘" + ep.Address.ToString() + ":" + ep.Port + "’"), ep); }
public virtual void OnUdpDataReceived(UdpEventArgs e) { EventHandler <UdpEventArgs> handler = UdpDataReceived; if (handler != null) { handler(this, e); } }
public void ReceiveCallback(IAsyncResult ar) { UdpClient udp = ((UdpState)(ar.AsyncState)).udp; IPEndPoint remoteEP = ((UdpState)(ar.AsyncState)).rep; byte[] receiveBytes = udp.EndReceive(ar, ref remoteEP); if (receiveBytes.Length > 0) { UdpEventArgs args = new UdpEventArgs(); args.bdata = receiveBytes; OnUdpDataReceived(args); udp.BeginReceive(new AsyncCallback(ReceiveCallback), (UdpState)ar.AsyncState); } }