Hello i got this code from here to receive data on a multicast address
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
Console.WriteLine("Ready to receive…");
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
EndPoint ep = (EndPoint)iep;
sock.Bind(iep);
sock.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership,
new MulticastOption(IPAddress.Parse("224.100.0.1")));
byte[] data = new byte[1024];
int recv = sock.ReceiveFrom(data, ref ep);
string stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("received: {0} from: {1}", stringData, ep.ToString());
sock.Close();
Now this works when i receive a packet, then the sock.ReceiveFrom is triggered but after that the receiver shuts down (even if i remove the sock.Close line).
What i want is that the receiver displays or saves me the data but continue listening for more packets.
What would be a good way to do this?
You have to code some kind of loop or use the async methods BeginReceive/EndReceive.
As soon as your local sock variable gets out of scope, it might be collected by gc and get disposed.