Skip to content

Instantly share code, notes, and snippets.

@rlipscombe
Created June 24, 2013 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlipscombe/5852628 to your computer and use it in GitHub Desktop.
Save rlipscombe/5852628 to your computer and use it in GitHub Desktop.
Discovering empegs over IP, in C#
// To discover empegs connected over Ethernet, we need to broadcast a '?', and listen for the response
var dgram = new byte[] { 0x3F }; // ASCII '?'
var client = new UdpClient(new IPEndPoint(IPAddress.Any, 8300));
client.EnableBroadcast = true;
client.Client.ReceiveTimeout = 500;
client.Send(dgram, dgram.Length, new IPEndPoint(IPAddress.Broadcast, 8300));
const int retryCount = 10;
for (int i = 0; i < retryCount; ++i)
{
try
{
IPEndPoint from = null;
var bytes = client.Receive(ref from);
Console.WriteLine(BitConverter.ToString(bytes));
var response = Encoding.ASCII.GetString(bytes);
Console.WriteLine("Received {0} from {1}.", response, from);
}
catch (SocketException)
{
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment