Skip to content

Commit 5314e40

Browse files
print ip of connected master
1 parent 760b618 commit 5314e40

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/Modbus_TCP_Slave.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
#include "Modbus_TCP_Slave.hpp"
77

88
#include <algorithm>
9+
#include <arpa/inet.h>
10+
#include <cstring>
911
#include <netinet/in.h>
1012
#include <netinet/tcp.h>
13+
#include <sstream>
1114
#include <stdexcept>
1215
#include <sys/socket.h>
1316
#include <system_error>
@@ -107,12 +110,29 @@ void Slave::set_debug(bool debug) {
107110
}
108111
}
109112

110-
void Slave::connect_client() {
113+
std::string Slave::connect_client() {
111114
int tmp = modbus_tcp_accept(modbus, &socket);
112115
if (tmp < 0) {
113116
const std::string error_msg = modbus_strerror(errno);
114117
throw std::runtime_error("modbus_tcp_accept failed: " + error_msg);
115118
}
119+
120+
struct sockaddr_in peer_addr;
121+
socklen_t len = sizeof(peer_addr);
122+
tmp = getpeername(modbus_get_socket(modbus), reinterpret_cast<struct sockaddr *>(&peer_addr), &len);
123+
124+
if (tmp < 0) {
125+
const std::string error_msg = modbus_strerror(errno);
126+
throw std::runtime_error("getpeername failed: " + error_msg);
127+
}
128+
129+
char buffer[INET_ADDRSTRLEN];
130+
inet_ntop(peer_addr.sin_family, &peer_addr.sin_addr, buffer, sizeof(buffer));
131+
132+
std::ostringstream sstr;
133+
sstr << buffer << ':' << htons(peer_addr.sin_port);
134+
135+
return sstr.str();
116136
}
117137

118138
bool Slave::handle_request() {

src/Modbus_TCP_Slave.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ class Slave {
4444

4545
/*! \brief wait for client to connect
4646
*
47+
* @return ip of the connected client
4748
*/
48-
void connect_client();
49+
std::string connect_client();
4950

5051
/*! \brief wait for request from Master and generate reply
5152
*

src/main.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,18 @@ int main(int argc, char **argv) {
8888
("m,monitor",
8989
"output all incoming and outgoing packets to stdout")
9090
("r,reconnect",
91-
"do not terminate if Master disconnects.")
91+
"do not terminate if the Modbus master disconnects.")
9292
("byte-timeout",
9393
"timeout interval in seconds between two consecutive bytes of the same message. "
94-
"In most cases it is sufficient to set teh response timeout. "
94+
"In most cases it is sufficient to set the response timeout. "
9595
"Fractional values are possible.",
9696
cxxopts::value<double>())
9797
("response-timeout",
9898
"set the timeout interval in seconds used to wait for a response. "
99-
"When a byte timeout is set, if elapsed time for the first byte of response is longer than "
100-
"the given timeout, a timeout is detected. "
99+
"When a byte timeout is set, if the elapsed time for the first byte of response is longer "
100+
"than the given timeout, a timeout is detected. "
101101
"When byte timeout is disabled, the full confirmation response must be received before "
102-
"expiration of the response timeout."
102+
"expiration of the response timeout. "
103103
"Fractional values are possible.",
104104
cxxopts::value<double>())
105105
#ifdef OS_LINUX
@@ -231,8 +231,9 @@ int main(int argc, char **argv) {
231231
do {
232232
// connect client
233233
std::cerr << "Waiting for Master to establish a connection..." << std::endl;
234+
std::string client;
234235
try {
235-
slave->connect_client();
236+
client = slave->connect_client();
236237
} catch (const std::runtime_error &e) {
237238
if (!terminate) {
238239
std::cerr << e.what() << std::endl;
@@ -241,7 +242,7 @@ int main(int argc, char **argv) {
241242
break;
242243
}
243244

244-
std::cerr << "Master established connection." << std::endl;
245+
std::cerr << "Master (" << client << ") established connection." << std::endl;
245246

246247
// ========== MAIN LOOP ========== (handle requests)
247248
bool connection_closed = false;

0 commit comments

Comments
 (0)