From 6155cb046330b81482e60bdb9e74c0236a266d31 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Fri, 23 Feb 2024 11:15:45 -0500 Subject: [PATCH] Wrote code to null-terminate the string sent in sendAll, and the string received in recvAll --- sock.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sock.cpp b/sock.cpp index aa2cc04..2d190d7 100644 --- a/sock.cpp +++ b/sock.cpp @@ -57,7 +57,7 @@ void Sock::sendAll(std::string to_send) { else { while (total_bytes_sent < str_length) { /* 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).data(), str_length - total_bytes_sent, 0); + 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; } @@ -80,7 +80,9 @@ std::string Sock::recvAll() { bool has_been_read = false; if (this->protocol == ES_UDP) { - recvfrom(this->sock_fd, buffer, 100, 0, dest, &addrlen); + num_bytes_received = recvfrom(this->sock_fd, buffer, 100, 0, dest, &addrlen); + /* Null-terminate the string */ + *(buffer + num_bytes_received) = '\0'; string.append(std::string(buffer)); } /* For TCP sockets */