Created
June 24, 2013 19:12
-
-
Save rlipscombe/5852628 to your computer and use it in GitHub Desktop.
Discovering empegs over IP, in C#
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
// 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