I am a newbie to C#. My background is Embedded systems and currently working on a WPF application that connects to a number of WiFi accesspoints one after the other. Only one connection will be active at any time. The WPF application after connecting to a accesspoint, scans the multicast UDP packets sent to a multicast address by the connected accesspoint. The problem I am facing is when I change the accesspoint, I fail to receive multicast udp packets from the second accesspoint, to make it work I have to restart my computer and rerun the WPF application. The only way I can work around is restarting the computer every time I change the accesspoint. This is not feasible.
Is there a way to remove the reference to the multicast address joined in windows..? I am calling setsocketoption(ADD_MEMBERSHIP) and setsocketoption(DROP_MEMBERSHIP) when done, but this doesn't seem to be doing the job.
Here is my code to join multicast group
MulticastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress localIPAddr = IPAddress.Parse("192.168.0.20");
IPAddress mcastAddress = IPAddress.Parse("227.0.0.0");
mcastPort = 12225;
EndPoint localEP = (EndPoint)new IPEndPoint(localIPAddr, mcastPort);
mcastOption = new MulticastOption(mcastAddress, localIPAddr);
MulticastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true);
MulticastSocket.Bind(localEP);
MulticastSocket.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership,
mcastOption);
Code where I'm receiving multicast messages -
byte[] bytes = new Byte[100];
EndPoint remoteEP = (EndPoint)new IPEndPoint(IPAddress.Any, 0);
MulticastSocket.ReceiveFrom(bytes, ref remoteEP)
Code where I leave the multicast group-
MulticastSocket.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.DropMembership,
mcastOption);
Output of "netsh interface ip show joins" command in a windows cmd prompt, after leaving multicast group
Interface 19: WiFi
Scope References Last Address
---------- ---------- ---- ---------------------------------
0 0 Yes 227.0.0.0
I am really lost as to how to solve this issue. Please advice.
Thanks, Vinay