Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将.Net Core和listnen中的System.Net.IPAddress.Any替换为所有网络适配器_C#_Uwp_.net Core_Network Programming - Fatal编程技术网

C# 如何将.Net Core和listnen中的System.Net.IPAddress.Any替换为所有网络适配器

C# 如何将.Net Core和listnen中的System.Net.IPAddress.Any替换为所有网络适配器,c#,uwp,.net-core,network-programming,C#,Uwp,.net Core,Network Programming,我正在尝试将一个VB.net项目从.net framework移植到.net核心和新的Windows通用平台应用程序中 在网络代码中,经常使用.IPAddress.Any,以便能够向所有网络适配器列出N。有人知道在.net core中有一种方法可以使用DatagramSockets监听所有网络适配器吗?IPAddress。任何都相当于IPAddress ip=IPAddress.Parse(“0.0.0.0”) 使用Windows.Networking.Sockets.DatagramSocke

我正在尝试将一个VB.net项目从.net framework移植到.net核心和新的Windows通用平台应用程序中


在网络代码中,经常使用.IPAddress.Any,以便能够向所有网络适配器列出N。有人知道在.net core中有一种方法可以使用DatagramSockets监听所有网络适配器吗?

IPAddress。任何
都相当于
IPAddress ip=IPAddress.Parse(“0.0.0.0”)

使用Windows.Networking.Sockets.DatagramSocket(Windows 8+) 使用System.Net.Sockets.Socket(.Net Core和UWP)
这是否意味着,当我想要创建新的数据套接字时,我可以使用:datagramSocket dg=newdatagramsocket();dg.BindEndpointAsync(新主机名(“0.0.0.0”,服务器端口);我的理解是,
DatagramSocket
不是.NET Core的一部分。无论如何,我已经用示例更新了我的答案,看一看。好吧,自从我开始开发windows UWP应用程序以来,我必须说我发现msdn文档非常混乱,难以浏览。但从我的角度来看,Datagramsockets似乎在.NET中可用core和UWP应用程序。我在raspberry PI:s上的c#和VB中都使用过它们。但我不能使用System.Net.Sockets.Socket命名空间。当我搜索msdn时,它会说它是.Net framework的一部分,与.Net core不同。令人困惑的是,.Net core不是.Net framework的一部分
private void Foo()
{
    datagramSocket = new DatagramSocket();
    datagramSocket.MessageReceived += OnMessageReceived;
    await datagramSocket.BindServiceNameAsync("8081");
}

private void OnMessageReceived(
    DatagramSocket sender,
    DatagramSocketMessageReceivedEventArgs args)
{
    // ...
}
private void Foo()
{
    Socket udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

    IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 8081);

    udpSocket.Bind(remoteEndPoint);

    SocketAsyncEventArgs e = new SocketAsyncEventArgs();
    byte[] buffer = new byte[1024];
    e.SetBuffer(buffer, 0, buffer.Length);
    e.RemoteEndPoint = remoteEndPoint;
    e.Completed += ReceiveFromCallback;

    if (!udpSocket.ReceiveFromAsync(e))
    {
        ReceiveFromCallback(udpSocket, e);
    }
}

private void ReceiveFromCallback(object sender, SocketAsyncEventArgs e)
{
    if (e.SocketError != SocketError.Success)
    {
        Debug.WriteLine(e.SocketError);
        return;
    }

    Socket udpSocket = sender as Socket;

    byte[] buffer = e.Buffer;
    Debug.WriteLine(Encoding.UTF8.GetString(buffer, 0, e.BytesTransferred));

    if (!udpSocket.ReceiveFromAsync(e))
    {
        ReceiveFromCallback(udpSocket, e);
    }
}