Skip to content

Commit 8d311bf

Browse files
committed
Improved debug error messages when connecting fails
1 parent e28bae5 commit 8d311bf

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

mqtt-sn.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,33 @@ int mqtt_sn_create_socket(const char* host, const char* port)
108108
If socket(2) (or connect(2)) fails, we (close the socket and)
109109
try the next address. */
110110
for (rp = result; rp != NULL; rp = rp->ai_next) {
111+
char hoststr[NI_MAXHOST] = "";
112+
int error = 0;
113+
114+
// Display the IP address in debug mode
115+
error = getnameinfo(rp->ai_addr, rp->ai_addrlen,
116+
hoststr, sizeof(hoststr), NULL, 0,
117+
NI_NUMERICHOST | NI_NUMERICSERV);
118+
if (error == 0) {
119+
mqtt_sn_log_debug("Trying %s...", hoststr);
120+
} else {
121+
mqtt_sn_log_debug("getnameinfo: %s", gai_strerror(ret));
122+
}
123+
124+
// Create a socket
111125
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
112-
if (fd == -1)
126+
if (fd == -1) {
127+
mqtt_sn_log_debug("Failed to create socket: %s", strerror(errno));
113128
continue;
129+
}
114130

115131
// Connect socket to the remote host
116-
if (connect(fd, rp->ai_addr, rp->ai_addrlen) == 0)
117-
break; // Success
132+
if (connect(fd, rp->ai_addr, rp->ai_addrlen) == 0) {
133+
// Success
134+
break;
135+
} else {
136+
mqtt_sn_log_debug("Connect failed: %s", strerror(errno));
137+
}
118138

119139
close(fd);
120140
}

0 commit comments

Comments
 (0)