Created
January 9, 2012 10:37
-
-
Save hasantayyar/1582416 to your computer and use it in GitHub Desktop.
UDPListener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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