簡體   English   中英

無法讓 UDP.BeginReceive 在 WPF 中工作

[英]Unable to get UDP.BeginReceive to work in WPF

我使用沒有問題UDPClientBeginReceive方法從控制台應用程序。 但是,我無法從我的 WPF 應用程序中運行它。 我很困惑。 我假設它與我從哪里調用此代碼有關。 我是 WPF 的新手。 我對 WPF 中UDPClient的綁定有什么誤解嗎?

是否從應用程序繼承,以便可能干擾 UDPClient BeginReceive()

現在,此代碼調用堆棧從 App.xaml.cs 轉到ButtonClick() ,它創建MachineItem的實例並調用StartMachine 沒有奇怪的線程或任何正在發生的事情(除非 WPF 在單擊按鈕期間做了一些我不知道的事情)。 再次調用BeginReceive() ,但如果我在Receive(IAsyncResult res)放置斷點,則不會發生任何事情。

public partial class App : Application
{
    //... all the normal stuff here
    private void Button_Click(object sender, RoutedEventArgs e)
    {

        MachineItem currentMachine = (MachineItem)(sender as Button).DataContext;
        currentMachine.StartMachine();
    }
}

現在單擊按鈕后,會發生這種情況:

public class MachineItem : INotifyPropertyChanged
{
    IPEndPoint sendersEndPoint;
    UdpClient listener;

    public void StartMachine()
    {
        listener = new UdpClient
        {
            ExclusiveAddressUse = false
        };

        listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        IPAddress ip = System.Net.IPAddress.Parse(this.LocalNIC_IPAddress);
        int ipInt = BitConverter.ToInt32(ip.GetAddressBytes(), 0);
        IPEndPoint endPoint = new IPEndPoint(ipInt, 60811); //new IPEndPoint(IPAddress.Any, listenPort); this might allow me not to know the IP of this local device!
        listener.Client.Bind(endPoint);
        listener.BeginReceive(new AsyncCallback(Receive), null);
        Thread.Sleep(20000); // this is just for testing purposes
    }

    private void Receive(IAsyncResult res)
    {
        byte[] bytes = listener.EndReceive(res, ref sendersEndPoint);
        listener.BeginReceive(new AsyncCallback(Receive), null);
        Console.WriteLine("Received");
    }
}

更新:我嘗試將這個 UDP 代碼放在應用程序首先開始的地方,以進一步縮小問題的范圍。 我仍然得到完全相同的行為 - 客戶端能夠調用 BeginReceive(),但是即使 UDP 數據包進來也沒有任何反應,換句話說,從未調用異步方法 Receive()...

public partial class WVGUIApp : Application
    {
    IPEndPoint sendersEndPoint;
    UdpClient listener;

    void AppStartup(object sender, StartupEventArgs args)
    {
        LoadMachineData();
        MainWindow mainWindow = new MainWindow();
        mainWindow.Show();

        listener = new UdpClient
        {
            ExclusiveAddressUse = false
        };
        listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        IPAddress ip = System.Net.IPAddress.Parse("10.178.100.111");
        int ipInt = BitConverter.ToInt32(ip.GetAddressBytes(), 0);
        IPEndPoint endPoint = new IPEndPoint(ipInt, 60811); //new IPEndPoint(IPAddress.Any, listenPort); this might allow me not to know the IP of this local device!
        listener.Client.Bind(endPoint);
        listener.BeginReceive(new AsyncCallback(Receive), null);
        //Thread.Sleep(20000);

    }

    private void Receive(IAsyncResult res)
    {
        byte[] bytes = listener.EndReceive(res, ref sendersEndPoint);
        listener.BeginReceive(new AsyncCallback(Receive), null);
        Console.WriteLine("Received");
    }
}

經過多天的斗爭,我發現了這個問題。 Windows 防火牆不允許應用程序的調試版本通過。 這對我來說很難診斷,因為 UDPClient.Send() 是允許的,但 UDPClient.BeginReceive() 不允許通過防火牆。 通過轉到控制面板\\所有控制面板項目\\Windows Defender 防火牆\\允許的應用程序\\ 並單擊應用程序的名稱,可以在 Windows 10 上輕松解決此問題。 允許通過發布版本,但調試具有與發布不同的條目。 感謝 MM8 對健全性檢查的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM