[C#] using System; using System.Net; using System.Net.Sockets; using System.Text;
public class UDPMulticastListener {
private static readonly IPAddress GroupAddress = IPAddress.Parse("224.168.100.2"); private const int GroupPort = 11000;
private static void StartListener() { bool done = false;
UdpClient listener = new UdpClient(); IPEndPoint groupEP = new IPEndPoint(GroupAddress,GroupPort);
try { listener.JoinMulticastGroup(GroupAddress); listener.Connect(groupEP);
while (!done) { Console.WriteLine("Waiting for broadcast"); byte[] bytes = listener.Receive( ref groupEP);
Console.WriteLine("Received broadcast from {0} :/n {1}/n", groupEP.ToString(), Encoding.ASCII.GetString(bytes,0,bytes.Length)); }
listener.Close();
} catch (Exception e) { Console.WriteLine(e.ToString()); }
}
public static int Main(String[] args) { StartListener();
return 0; } } |