コード例 #1
0
        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);
        }
コード例 #2
0
    /////////////////////
    ///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);
    }
コード例 #3
0
        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);
        }
コード例 #4
0
    public virtual void OnUdpDataReceived(UdpEventArgs e)
    {
        EventHandler <UdpEventArgs> handler = UdpDataReceived;

        if (handler != null)
        {
            handler(this, e);
        }
    }
コード例 #5
0
    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);
        }
    }