From 1359f22f71e9fb4380f836437ef50caf1d615f5e Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Tue, 6 Feb 2024 07:50:18 -0500 Subject: [PATCH] Added address parameter to constructor, and added additional error handling --- includes/server.hpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/includes/server.hpp b/includes/server.hpp index 6f933ca..c5f10a3 100644 --- a/includes/server.hpp +++ b/includes/server.hpp @@ -1,6 +1,7 @@ #include "easysock.hpp" #include - +#include +#include "exception_consts.hpp" /* Server class - Defines a TCP/UDP server. - The constructor takes in a **local** address and port, on which the server listens. @@ -17,12 +18,17 @@ private: void create_socket() { struct sockaddr* dest = (struct sockaddr *)malloc(sizeof(struct sockaddr)); this->sock_fd = create_local(this->ip_ver, this->protocol, this->address.data(), this->port, dest); + if (sock_fd < 0) { + if (sock_fd * -1 == ECONNREFUSED) { + throw EXCEPT_CONNREFUSED; + } + } } public: Server() {} - Server(int ip_ver, char protocol, int port) { + Server(int ip_ver, char protocol, const char* address, int port) { /* Error checking */ if (ip_ver != 4 && ip_ver != 6) { throw std::invalid_argument("Invalid IP address type"); @@ -37,13 +43,18 @@ public: this->ip_ver = ip_ver; this->protocol = protocol; this->port = port; - if (ip_ver == 4) { - this->address = "127.0.0.1"; - } else if (ip_ver == 6) { - this->address = "::1"; + this->address = std::string(address); + + /* Check to see if the given IP address matches the given ip_ver */ + if ((check_ip_ver(address.data() == 4 && ip_ver == 6) || (chcek_ip_ver(address.data() == 6 && address == 4)) { + throw std::invalid_argument("Invalid IP address for given type."); } - create_socket(); + try { + create_socket(); + } catch (int e) { + throw; + } }