public static bool ScanGrainBrains() { bool ret = false; var Client = new UdpClient(); var ServerEp = new IPEndPoint(IPAddress.Any, 8888); var raw = new byte[1024]; NetworkDiscoveryRequest request = new NetworkDiscoveryRequest(); request.Name = "NETWORK-DISCOVERY-MSG"; var ms = new MemoryStream(); request.WriteTo(ms); Client.EnableBroadcast = true; Client.Send(ms.GetBuffer(), (int)ms.Length, new IPEndPoint(IPAddress.Broadcast, 8888)); var rawr = new byte[15000]; var AsyncResponseData = Client.BeginReceive(null, null); int timeToWait = 1000; AsyncResponseData.AsyncWaitHandle.WaitOne(timeToWait); if (AsyncResponseData.IsCompleted) { try { byte[] receivedData = Client.EndReceive(AsyncResponseData, ref ServerEp); //Receive(ref ServerEp); var msr = new MemoryStream(receivedData); NetworkDiscoveryReply reply = new NetworkDiscoveryReply(); reply.MergeDelimitedFrom(msr); Console.Out.WriteLine("Grainbrain is found at {0}", ServerEp.Address); ipAddr = ServerEp.Address; ret = true; // EndReceive worked and we have received data and remote endpoint } catch (Exception ex) { Console.WriteLine("Caught exception {0}", ex); // EndReceive failed and we ended up here } } else { // The operation wasn't completed before the timeout and we're off the hook } Client.Close(); if (ret) { updateStatus(); } return(ret); }
public static bool ScanGrainBrains() { bool ret = false; var Client = new UdpClient(); var ServerEp = new IPEndPoint(IPAddress.Any, 8888); var raw = new byte[1024]; NetworkDiscoveryRequest request = new NetworkDiscoveryRequest(); request.Name = "NETWORK-DISCOVERY-MSG"; var ms = new MemoryStream(); request.WriteTo(ms); Client.EnableBroadcast = true; Client.Send(ms.GetBuffer(), (int)ms.Length, new IPEndPoint(IPAddress.Broadcast, 8888)); var rawr = new byte[15000]; var AsyncResponseData = Client.BeginReceive(null, null); int timeToWait = 1000; AsyncResponseData.AsyncWaitHandle.WaitOne(timeToWait); if (AsyncResponseData.IsCompleted) { try { byte[] receivedData = Client.EndReceive(AsyncResponseData, ref ServerEp); //Receive(ref ServerEp); var msr = new MemoryStream(receivedData); NetworkDiscoveryReply reply = new NetworkDiscoveryReply(); reply.MergeDelimitedFrom(msr); Console.Out.WriteLine("Grainbrain is found at {0}", ServerEp.Address); ipAddr = ServerEp.Address; ret = true; // EndReceive worked and we have received data and remote endpoint } catch (Exception ex) { Console.WriteLine("Caught exception {0}", ex); // EndReceive failed and we ended up here } } else { // The operation wasn't completed before the timeout and we're off the hook } Client.Close(); if (ret) updateStatus(); return ret; }