Added some error checking

This commit is contained in:
Trimutex 2023-10-02 13:10:33 -05:00
parent 9e94f77a83
commit 5e0069871e

View File

@ -1,6 +1,7 @@
#include "network.hpp" #include "network.hpp"
#include <arpa/inet.h> #include <arpa/inet.h>
#include <stdlib.h> // Needed for exit() #include <stdlib.h> // Needed for exit()
#include <string.h> // Needed for strcpy() and strlen()
#include <iostream> #include <iostream>
void HandleClient(int browser_s, int server_s) { void HandleClient(int browser_s, int server_s) {
@ -9,16 +10,24 @@ void HandleClient(int browser_s, int server_s) {
ssize_t buf_len; // Buffer length for file reads ssize_t buf_len; // Buffer length for file reads
// Pass GET along from browser to server // Pass GET along from browser to server
std::cout << "Attempting to send to server" << std::endl;
do { do {
buf_len = recv(browser_s, in_buf, BUF_SIZE, 0); buf_len = recv(browser_s, in_buf, BUF_SIZE, 0);
if (buf_len == -1) {
std::cout << "ERROR - recv returned -1" << std::endl;
std::terminate();
}
send(server_s, in_buf, buf_len, 0); send(server_s, in_buf, buf_len, 0);
} while (buf_len != 0); } while (buf_len != 0);
std::cout << "Sent to server" << std::endl;
// Pass response along from server to browser // Pass response along from server to browser
std::cout << "Attempting to send to browser" << std::endl;
do { do {
buf_len = recv(server_s, out_buf, BUF_SIZE, 0); buf_len = recv(server_s, out_buf, BUF_SIZE, 0);
send(browser_s, out_buf, buf_len, 0); send(browser_s, out_buf, buf_len, 0);
} while (buf_len != 0); } while (buf_len != 0);
std::cout << "Sent to browser" << std::endl;
close(server_s); close(server_s);
close(browser_s); close(browser_s);
} }