From 079cdafe0f55898de6490349fbf8fd2b6cb4a7b5 Mon Sep 17 00:00:00 2001 From: kzhioki Date: Thu, 12 Dec 2019 09:50:33 +0900 Subject: [PATCH] Add error recovery when UDP transfer fails This commit does not affect normal case. Even if UDP transfer fails for any reasons in sendNTPPacket, it can be recovered by re-initialization. --- NTPClient.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index 760e142..5ed96e1 100755 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -201,7 +201,11 @@ void NTPClient::sendNTPPacket() { this->_udp->beginPacket(this->_poolServerIP, 123); } this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE); - this->_udp->endPacket(); + if (this->_udp->endPacket() == 0) { + // recovery if UDP client encounters any errors + this->end(); + this->begin(this->_port); + } } void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) {