Skip to content

Commit 7a11cf0

Browse files
committed
SocketWrapper: call close() before mis-reusing connection
Some libraries don't call close() and expect the device will do it on its own when the server closes the connection. To fix this, the best thing to do would be either to use connectionKeepAlive() or to simply close() the client before conneting again. For backwards compatibility, let's start from scratch every time connect() is colled twice.
1 parent 9619ec8 commit 7a11cf0

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

libraries/SocketWrapper/src/MbedClient.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ void arduino::MbedClient::configureSocket(Socket *_s) {
7878
}
7979

8080
int arduino::MbedClient::connect(SocketAddress socketAddress) {
81+
82+
if (sock && reader_th) {
83+
// trying to reuse a connection, let's call stop() to cleanup the state
84+
char c;
85+
if (sock->recv(&c, 1) < 0) {
86+
stop();
87+
}
88+
}
89+
8190
if (sock == nullptr) {
8291
sock = new TCPSocket();
8392
_own_socket = true;

0 commit comments

Comments
 (0)