From 54f7dbe7ee0e086182a3ab26dfb069c6aaee19fd Mon Sep 17 00:00:00 2001 From: Rockingcool Date: Tue, 12 Mar 2024 00:13:56 -0500 Subject: [PATCH] Modified recvAllNB() to return an empty string (instead of NULL) if there is nothing to read --- includes/sock.hpp | 3 ++- sock.cpp | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/includes/sock.hpp b/includes/sock.hpp index 6206b4b..715260b 100644 --- a/includes/sock.hpp +++ b/includes/sock.hpp @@ -49,7 +49,8 @@ public: /* Method to receive data sent to the 'other_socket' socket */ char* recvAll(); - /* Non-blocking receive method - calls the method above after polling for data */ + /* Non-blocking receive method - calls the method above after polling for data. Returns + an empty string if there is nothing to read. */ char* recvAllNB(); /* Returns socket identifier */ diff --git a/sock.cpp b/sock.cpp index 9963757..24cde91 100644 --- a/sock.cpp +++ b/sock.cpp @@ -81,7 +81,7 @@ This function also needs more testing for TCP. */ char* Sock::recvAll() { int num_bytes_received = 0; int total_bytes_received = 0; - char* buffer = (char *)malloc(100); + char* buffer = (char *)malloc(150); bool has_been_read = false; if (this->protocol == ES_UDP) { @@ -117,7 +117,8 @@ char* Sock::recvAll() { return buffer; } -/* Non-blocking recv call - Uses 'select' to poll for data from the FD. */ +/* Non-blocking recv call - Uses 'select' to poll for data from the FD. Returns an empty string if there +is nothing to read. */ char* Sock::recvAllNB() { struct timeval tv; fd_set readfs; @@ -131,7 +132,7 @@ char* Sock::recvAllNB() { if (FD_ISSET(this->sock_fd, &readfs)) { return Sock::recvAll(); } else { - return NULL; + return (char *)""; } }