I am trying to receive the UPnP NOTIFY Messages from UPnP Devices in my Network. But when i send the M-SEARCH Message, i sometimes get no Answers. My Code looks like this:
public bool StartListener()
{
if (this.ssdpSocket == null)
{
IPAddress localIpAddress = IPAddress.Any;
IPEndPoint localIpEndpoint = new IPEndPoint(localIpAddress, SsdpPort);
try
{
this.ssdpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
this.ssdpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
this.ssdpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 16384);
this.ssdpSocket.Bind(localIpEndpoint);
this.ssdpSocket.SetSocketOption(
SocketOptionLevel.IP,
SocketOptionName.AddMembership,
new MulticastOption(IPAddress.Parse(SsdpMulticastAddress), localIpAddress));
this.ssdpSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 2);
this.ssdpSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, true);
this.culture = Thread.CurrentThread.CurrentUICulture;
this.workerThreadListener = new WorkerThread(this.ssdpSocket, this.HandleSsdpMessage);
this.workerThreadListener.Start();
Log.InfoFormat("SSDP server bind successful [{0}]", localIpEndpoint);
return true;
}
catch (Exception exception)
{
Log.Info(string.Format("SSDP server bind failed [{0}]", localIpEndpoint), exception);
throw;
}
}
return false;
}
I have found following answer but for me it is not possible to change the port. Is there an alternative solution?
AddMembership
call to hard-code port 1900 rather than using the port fromlocalIpAddress
– simonc