From d842485103ae9617ab7b422ac02a72b48d626e83 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Mon, 18 Mar 2024 13:43:09 -0400 Subject: [PATCH] Throw errno instead of errno * -1, if an error is encountered with sending or receiving --- sock.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/sock.cpp b/sock.cpp index c01bef5..7bc3d16 100644 --- a/sock.cpp +++ b/sock.cpp @@ -50,7 +50,9 @@ void Sock::sendAll(std::string to_send) { /* For UDP sockets */ if (this->protocol == ES_UDP) { - sendto(this->sock_fd, to_send.data(), str_length, 0, (struct sockaddr *)dest, addrlen); + if (sendto(this->sock_fd, to_send.data(), str_length, 0, (struct sockaddr *)dest, addrlen) == -1) { + throw errno; + } } /* For TCP sockets */ else { @@ -58,7 +60,7 @@ void Sock::sendAll(std::string to_send) { /* Send the data to the 'other_socket' variable, which should be set by the client and server methods */ num_bytes_sent = send(this->other_socket, to_send.substr(total_bytes_sent).c_str(), str_length - total_bytes_sent, 0); if (num_bytes_sent < 0) { - throw errno * -1; + throw errno; } total_bytes_sent += num_bytes_sent; } @@ -89,6 +91,9 @@ char* Sock::recvAll() { if (num_bytes_received == 0) { return NULL; } + if (num_bytes_received < 0) { + throw errno; + } /* Null-terminate the string */ *(buffer + num_bytes_received) = '\0'; return buffer; @@ -106,7 +111,7 @@ char* Sock::recvAll() { } if (num_bytes_received < 0) { - throw errno * -1; + throw errno; } total_bytes_received += num_bytes_received; has_been_read = true;