C# 框架中的 UDP 数据报套接字(来自 UWP 的端口)

Posted

技术标签:

【中文标题】C# 框架中的 UDP 数据报套接字(来自 UWP 的端口)【英文标题】:UDP Datagram Socket in C# Framework (port from UWP) 【发布时间】:2017-11-08 18:07:50 【问题描述】:

我正在尝试将以下代码从 UWP 移植到 C# 框架(WPF 应用程序)

public class DatagramSocket : IDatagramSocket

    private Windows.Networking.Sockets.DatagramSocket socket;

    public DatagramSocket()
    
        socket = new Windows.Networking.Sockets.DatagramSocket();
        socket.MessageReceived += Socket_MessageReceived;
    

    public event Action<DatagramSocketMessage> MessageReceived;

    public async Task Bind(string profile, int liveViewPort)
    
        Debug.WriteLine($"Binding to profile on port liveViewPort");
        await socket.BindEndpointAsync(new HostName(profile), liveViewPort.ToString());
    

    public void Dispose()
    
        socket.Dispose();
    

    private void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
    
        using (var reader = args.GetDataReader())
        
            var buf = new byte[reader.UnconsumedBufferLength];
            reader.ReadBytes(buf);
            MessageReceived?.Invoke(new DatagramSocketMessage(args.RemoteAddress.CanonicalName, buf));
        
    

我尝试了以下方法,但永远不会调用 OnUdpData 回调:

public class DatagramSocket : IDatagramSocket

    public UdpClient socket;
    public IPEndPoint ep;

    public event Action<DatagramSocketMessage> MessageReceived;

    public async Task Bind(string profile, int liveViewPort)
    
        socket = new UdpClient(profile, liveViewPort);
        socket.BeginReceive(OnUdpData, socket);
    

    void OnUdpData(IAsyncResult result)
    
        if (_isDisposed)
            return;

        var socket = result.AsyncState as UdpClient;

        // points towards whoever had sent the message:
        IPEndPoint source = new IPEndPoint(0, 0);
        // get the actual message and fill out the source:
        byte[] message = socket.EndReceive(result, ref source);

        MessageReceived?.Invoke(new DatagramSocketMessage(socket.ToString(), message));
        socket.BeginReceive(OnUdpData, socket);
    

    private volatile bool _isDisposed;
    public void Dispose()
    
        _isDisposed = true;
        socket.Dispose();
    

还尝试了一个不起作用的异步解决方案。

    public async Task Bind(string profile, int liveViewPort)
    
        Task.Run(async () =>
        
            using (var udpClient = new UdpClient(profile, liveViewPort))
            
                while (!_isDisposed)
                
                    var receivedResults = await udpClient.ReceiveAsync();
                    MessageReceived?.Invoke(new DatagramSocketMessage(udpClient.ToString(), receivedResults.Buffer));

                
            
        );
    

有什么建议吗?谢谢你

【问题讨论】:

【参考方案1】:

找到原因,是windows防火墙阻止访问。

【讨论】:

以上是关于C# 框架中的 UDP 数据报套接字(来自 UWP 的端口)的主要内容,如果未能解决你的问题,请参考以下文章

C中的两种方式UDP套接字

请问,如何用C#解析UDP数据包中的数据,其中UDP包中的信息是来自激光雷达采集到的距离、方位角等信息。

如何在同一本地/src 地址上创建多个 UDP 数据报通道/流

UDP和套接字,recvfrom()返回-1,资源暂时不可用

如何拆分接收到的 boost asio udp 套接字联合数据报

Java网络编程-第三节:UDP数据报套接字(DatagramSocket)编程