首页 > 解决方案 > 如何在 SignalR Hub C# 中接收 UDP 套接字数据

问题描述

我在我的 Web 项目中的 SignalR 中创建了一个集线器。在我的集线器类中,我想接收在端口号 4001 上发送的 udp 数据。请建议通过 udp 套接字接收数据的代码。我尝试使用如下所示的代码接收,但它给出错误“System.Net.Sockets.SocketException:'通常只允许使用每个套接字地址(协议/网络地址/端口)”

时钟集线器.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;

namespace RealTimeClock.App_Code
{
    public class ClockHub : Hub
    {
        public void getSocketData()
        {     
       // Here I want to receive udp socket data  on port 4001 

        // my code commented here did not work
            //UdpClient receivingUdpClient = new UdpClient(11000); 
            //IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
            //Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
            //string UDP_Rx_Data= Encoding.ASCII.GetString   rceiveBytes);  



            
            Clients.Caller.setTime(UDP_Rx_Data);
        }
    }
}

标签: c#signalr

解决方案


推荐阅读