@@ -136,6 +136,8 @@ namespace mtconnect::source::adapter::shdr {
136136 return true ;
137137 }
138138
139+ // / @brief Attempt to reconnect after a delay. If the server is a hostname, re-resolve it to get the current IP
140+ // / address in case it has changed. If the server is a static IP address, just reconnect.
139141 inline void Connector::asyncTryConnect ()
140142 {
141143 NAMED_SCOPE (" Connector::asyncTryConnect" );
@@ -145,7 +147,21 @@ namespace mtconnect::source::adapter::shdr {
145147 if (ec != boost::asio::error::operation_aborted)
146148 {
147149 LOG (info) << " reconnect: retrying connection" ;
148- asio::dispatch (m_strand, boost::bind (&Connector::connect, this ));
150+ // Re-resolve hostname to handle DHCP/dynamic IP environments.
151+ // If the server is a hostname (not a static IP), re-resolve to get
152+ // the current IP address in case it has changed.
153+ boost::system::error_code addrEc;
154+ ip::make_address (m_server, addrEc);
155+ if (addrEc)
156+ {
157+ // m_server is a hostname, re-resolve it
158+ asio::dispatch (m_strand, boost::bind (&Connector::resolve, this ));
159+ }
160+ else
161+ {
162+ // m_server is a static IP address, just reconnect
163+ asio::dispatch (m_strand, boost::bind (&Connector::connect, this ));
164+ }
149165 }
150166 });
151167 }
@@ -183,7 +199,16 @@ namespace mtconnect::source::adapter::shdr {
183199 }
184200 else
185201 {
186- LOG (info) << " Connected with: " << m_socket.remote_endpoint ();
202+ boost::system::error_code rec;
203+ auto remote = m_socket.remote_endpoint (rec);
204+ if (rec)
205+ {
206+ LOG (error) << " Failed to get remote endpoint: " << rec.message ();
207+ }
208+ else
209+ {
210+ LOG (info) << " Connected with: " << remote;
211+ }
187212 m_timer.cancel ();
188213
189214 m_socket.set_option (asio::ip::tcp::no_delay (true ));
0 commit comments