我試圖讓我的ASP.NET應用程序偵聽多播UDP廣播。不幸的是,由於權限/ API問題,我似乎陷入了束縛。ASP.NET多播UdpClient問題
問題是我需要允許應用程序的多個實例偵聽相同的IP /端口,因爲ASP.NET應用程序會發生多個spinup。爲此,必須將SocketOptionName.ReuseAddress設置爲true。問題是,這需要我的ASP.NET應用程序不應具有的管理權限。
下面的代碼:
public static void Listen(int port)
{
var groupAddress = IPAddress.Parse("224.10.10.10");
var endPoint = new IPEndPoint(groupAddress, port);
var client = new UdpClient();
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
client.Client.Bind(new IPEndpoint(IPAddress.Any, port)); // Error thrown here
client.JoinMulticastGroup(groupAddress);
var udpState = new UdpState() { Client = client, EndPoint = endPoint };
client.BeginReceive(OnMessageReceived, udpState); // OnMessageReceived code omitted
}
你使用什麼端口號?不確定Windows,但unix系統通常需要管理員權限,當綁定端口1024以下,但不超過1024. – Cellfish 2010-01-08 06:51:23
端口高,65008ish。它似乎是框架的一個限制。如果我這樣做:var client = new UdpClient(port),它不需要管理員權限,但是它也不能與ReuseAddress套接字選項一起使用。 – 2010-01-08 14:27:10