Skip to content

Commit

Permalink
Merge pull request #47 from Auterion/tcp-client-timeout
Browse files Browse the repository at this point in the history
Add a timeout argument to TCPClient
  • Loading branch information
ThomasDebrunner authored Jun 13, 2024
2 parents e7544fb + 02e74b1 commit 7d82dca
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion include/mav/TCPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,19 @@ namespace mav {

public:

TCPClient(const std::string& address, int port) {
TCPClient(const std::string& address, int port, int timeout = -1) {
_socket = socket(AF_INET, SOCK_STREAM, 0);
if (_socket < 0) {
throw NetworkError("Could not create socket", errno);
}

if (timeout > 0) {
struct timeval send_timeout;
send_timeout.tv_sec = 0;
send_timeout.tv_usec = timeout * 1000; // timeout is in ms
setsockopt(_socket, SOL_SOCKET, SO_SNDTIMEO, &send_timeout, sizeof(send_timeout));
}

struct hostent *hp;
hp = gethostbyname(address.c_str());
if (hp == nullptr) {
Expand Down

0 comments on commit 7d82dca

Please sign in to comment.