Wrote code to null-terminate the string sent in sendAll, and the string received in recvAll
This commit is contained in:
6
sock.cpp
6
sock.cpp
@@ -57,7 +57,7 @@ void Sock::sendAll(std::string to_send) {
|
|||||||
else {
|
else {
|
||||||
while (total_bytes_sent < str_length) {
|
while (total_bytes_sent < str_length) {
|
||||||
/* Send the data to the 'other_socket' variable, which should be set by the client and server methods */
|
/* 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) {
|
if (num_bytes_sent < 0) {
|
||||||
throw errno * -1;
|
throw errno * -1;
|
||||||
}
|
}
|
||||||
@@ -80,7 +80,9 @@ std::string Sock::recvAll() {
|
|||||||
bool has_been_read = false;
|
bool has_been_read = false;
|
||||||
|
|
||||||
if (this->protocol == ES_UDP) {
|
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));
|
string.append(std::string(buffer));
|
||||||
}
|
}
|
||||||
/* For TCP sockets */
|
/* For TCP sockets */
|
||||||
|
Reference in New Issue
Block a user