Skip to content

Instantly share code, notes, and snippets.

@hasantayyar
Created January 9, 2012 10:37
Show Gist options
  • Save hasantayyar/1582416 to your computer and use it in GitHub Desktop.
Save hasantayyar/1582416 to your computer and use it in GitHub Desktop.
UDPListener
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class UDPListener
{
private const int listenPort = 11000;
private static void StartListener()
{
bool done = false;
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any,listenPort);
try
{
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));
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
listener.Close();
}
}
public static int Main()
{
StartListener();
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment