Commit 4f2efcab0395b3bc30dda51656c1fcc0707a5876
Committed by
Moritz Wirger
1 parent
2c40066e
Make sure to close sockets on errors and after receiving data
Showing
1 changed file
with
4 additions
and
1 deletions
hueplusplus/winHttpHandler.cpp
| @@ -95,6 +95,7 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad | @@ -95,6 +95,7 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad | ||
| 95 | // Send an initial buffer | 95 | // Send an initial buffer |
| 96 | if (::send(connect_socket, msg.c_str(), msg.size(), 0) == SOCKET_ERROR) | 96 | if (::send(connect_socket, msg.c_str(), msg.size(), 0) == SOCKET_ERROR) |
| 97 | { | 97 | { |
| 98 | + closesocket(connect_socket); | ||
| 98 | std::cerr << "winHttpHandler: send failed: " << WSAGetLastError() << std::endl; | 99 | std::cerr << "winHttpHandler: send failed: " << WSAGetLastError() << std::endl; |
| 99 | throw(std::runtime_error("winHttpHandler: send failed")); | 100 | throw(std::runtime_error("winHttpHandler: send failed")); |
| 100 | } | 101 | } |
| @@ -128,10 +129,12 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad | @@ -128,10 +129,12 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad | ||
| 128 | } | 129 | } |
| 129 | else | 130 | else |
| 130 | { | 131 | { |
| 132 | + closesocket(connect_socket); | ||
| 131 | std::cerr << "winHttpHandler: recv failed: " << WSAGetLastError() << std::endl; | 133 | std::cerr << "winHttpHandler: recv failed: " << WSAGetLastError() << std::endl; |
| 132 | throw(std::runtime_error("winHttpHandler: recv failed")); | 134 | throw(std::runtime_error("winHttpHandler: recv failed")); |
| 133 | } | 135 | } |
| 134 | } while (res > 0); | 136 | } while (res > 0); |
| 137 | + closesocket(connect_socket); | ||
| 135 | 138 | ||
| 136 | return response; | 139 | return response; |
| 137 | } | 140 | } |
| @@ -202,8 +205,8 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, | @@ -202,8 +205,8 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, | ||
| 202 | // Send a message to the multicasting address. | 205 | // Send a message to the multicasting address. |
| 203 | if (sendto(connect_socket, msg.c_str(), msg.size(), 0, (struct sockaddr FAR *) &dest_sin, sizeof(dest_sin)) == SOCKET_ERROR) | 206 | if (sendto(connect_socket, msg.c_str(), msg.size(), 0, (struct sockaddr FAR *) &dest_sin, sizeof(dest_sin)) == SOCKET_ERROR) |
| 204 | { | 207 | { |
| 205 | - std::cerr << "winHttpHandler: sendMulticast: sendto failed: " << WSAGetLastError() << std::endl; | ||
| 206 | closesocket(connect_socket); | 208 | closesocket(connect_socket); |
| 209 | + std::cerr << "winHttpHandler: sendMulticast: sendto failed: " << WSAGetLastError() << std::endl; | ||
| 207 | throw(std::runtime_error("winHttpHandler: sendMulticast: sendto failed")); | 210 | throw(std::runtime_error("winHttpHandler: sendMulticast: sendto failed")); |
| 208 | } | 211 | } |
| 209 | 212 |