public void SendRigBusInfo() { var ServerEp = new IPEndPoint(IPAddress.Any, 0); DirGreetingList dirList = DirGreetingList.Instance; udpClient.EnableBroadcast = true; udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000); while (true) { try { rigBusDesc.Time = DateTimeUtils.ConvertToUnixTime(DateTime.Now); Byte[] senddata = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(rigBusDesc)); udpClient.Send(senddata, senddata.Length, new IPEndPoint(IPAddress.Broadcast, 7300)); var ServerResponseData = udpClient.Receive(ref ServerEp); var ServerResponse = Encoding.ASCII.GetString(ServerResponseData); var dirService = DirectoryBusGreeting.ParseCommand(ServerResponse); DirGreetingList.Instance.Add(dirService); Thread.Sleep(HamBusEnv.SleepTimeMs); } catch (SocketException) { Console.WriteLine("Timeout: Maybe DirectoryBus isn't running! "); } catch (Exception e) { Console.WriteLine("Timeout: Maybe DirectoryBus isn't running! \nExceptions: {0}", e.ToString()); } } }
private IPEndPoint SendUpdateResponse(IPEndPoint ServerEp) { Byte[] senddata = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(rigBusDesc)); rigBusDesc.Time = DateTimeUtils.ConvertToUnixTime(DateTime.Now); udpClient.Send(senddata, senddata.Length, new IPEndPoint(IPAddress.Broadcast, 7300)); Console.WriteLine("Sending Data"); var ServerResponseData = udpClient.Receive(ref ServerEp); var ServerResponse = Encoding.ASCII.GetString(ServerResponseData); Console.WriteLine("Recived {0} from {1} port {2}", ServerResponse, ServerEp.Address.ToString(), ServerEp.Port); var dirService = DirectoryBusGreeting.ParseCommand(ServerResponse); DirGreetingList.Instance.Add(dirService); Thread.Sleep(3000); return(ServerEp); }
public void SendRigBusInfo() { //https://stackoverflow.com/questions/2281441/can-i-set-the-timeout-for-udpclient-in-c int count = 0; var timeToWait = TimeSpan.FromSeconds(10); var ServerEp = new IPEndPoint(IPAddress.Any, 0); DirGreetingList dirList = DirGreetingList.Instance; udpClient.EnableBroadcast = true; udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000); while (true) { try { dataBusDesc.Time = DateTimeUtils.ConvertToUnixTime(DateTime.Now); Byte[] senddata = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(dataBusDesc)); udpClient.Send(senddata, senddata.Length, new IPEndPoint(IPAddress.Broadcast, 7300)); Console.WriteLine("Sending Data {0}", count++); var ServerResponseData = udpClient.Receive(ref ServerEp); var ServerResponse = Encoding.ASCII.GetString(ServerResponseData); var dirService = DirectoryBusGreeting.ParseCommand(ServerResponse); DirGreetingList.Instance.Add(dirService); Thread.Sleep(3000); } catch (SocketException se) { HamBusEnv.Logger.LogInformation($"Socket Error! Maybe Directory Bus isn't running.\n {se.Message}"); } catch (Exception e) { HamBusEnv.Logger.LogInformation($"socket {e.Message}"); // Console.WriteLine("Exception: {0}", e.Message); //infoThread.Abort(); } } }