Skip to content

Instantly share code, notes, and snippets.

@Pranit-Harekar
Last active August 26, 2021 16:34
Show Gist options
  • Save Pranit-Harekar/d9a3a1b804988a0c8d929d3dc50cee51 to your computer and use it in GitHub Desktop.
Save Pranit-Harekar/d9a3a1b804988a0c8d929d3dc50cee51 to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
using System.Text;
namespace datacap_demo
{
class Program
{
static void Main(string[] args)
{
int port = 9001;
var deviceID = "<device serial number 24 char>";
System.Net.Sockets.UdpClient udpClient = new System.Net.Sockets.UdpClient();
System.Net.IPEndPoint groupEP = new System.Net.IPEndPoint(System.Net.IPAddress.Any, port);
// Bind
udpClient.Client.Bind(groupEP);
// Listen
var listener = Task.Run(() =>
{
while (true)
{
byte[] recvBuffer = udpClient.Receive(ref groupEP);
var resp = Encoding.UTF8.GetString(recvBuffer);
if (resp.Contains(deviceID + " is at:"))
Console.WriteLine("Found DC Direct at: " + resp.Substring(resp.IndexOf(':') + 1));
}
});
// Broadcast
var data = Encoding.UTF8.GetBytes("Who has " + deviceID);
udpClient.Send(data, data.Length, "255.255.255.255", port);
listener.Wait(5000); // wait 5 seconds for response
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment