From 0058e7e4117da021729a084e646407cbe06c207b Mon Sep 17 00:00:00 2001 From: Rockingcool Date: Sun, 10 Mar 2024 21:53:06 -0500 Subject: [PATCH] Removed ip_ver parameter I removed this because I realized I could just check the IP version inside the constructor. The Sock constructor now checks the address passed to it. Like before, if the address is neither v4 nor v6, an exception is thrown. Since the Server and Client constructors call the Sock constructor, no change was required in these files, except passing the right number of parameters. --- includes/client.hpp | 2 +- includes/server.hpp | 2 +- includes/sock.hpp | 2 +- sock.cpp | 12 ++++-------- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/includes/client.hpp b/includes/client.hpp index 3c01942..fc66948 100644 --- a/includes/client.hpp +++ b/includes/client.hpp @@ -15,7 +15,7 @@ public: ~Client(); /* Normal constructor that calls the parent constructor to set the given values */ - Client(int ip_ver, char protocol, const char* address, int port) : Sock(ip_ver, protocol, address, port) {} + Client(char protocol, const char* address, int port) : Sock(protocol, address, port) {} void create_socket() override; diff --git a/includes/server.hpp b/includes/server.hpp index 1d22e5c..e8a2196 100644 --- a/includes/server.hpp +++ b/includes/server.hpp @@ -15,7 +15,7 @@ public: /* Constructors */ Server() {} - Server(int ip_ver, char protocol, const char* address, int port) : Sock(ip_ver, protocol, address, port) {} + Server(char protocol, const char* address, int port) : Sock(protocol, address, port) {} /* Destructor */ ~Server(); diff --git a/includes/sock.hpp b/includes/sock.hpp index b73eca3..1d2e7b7 100644 --- a/includes/sock.hpp +++ b/includes/sock.hpp @@ -38,7 +38,7 @@ public: virtual ~Sock(); /* Regular constructor - defined in sock.cpp */ - Sock(int ip_ver, char protocol, const char* address, int port); + Sock(char protocol, const char* address, int port); /* Method to send data in 'to_send' through the 'other_socket' socket */ void sendAll(std::string to_send); diff --git a/sock.cpp b/sock.cpp index b4842c9..91f366b 100644 --- a/sock.cpp +++ b/sock.cpp @@ -18,11 +18,12 @@ Sock::~Sock() {} /* Constructor - This function initializes the object attributes with the given -parameters. It throws an exception if an IPv4 address was given, but the type -given is IPv6 (or the other way around). */ +parameters. The address version (IPv4 or IPv6) is determined based on the given address. */ -Sock::Sock(int ip_ver, char protocol, const char* address, int port) { +Sock::Sock(char protocol, const char* address, int port) { /* Error checking */ + this->ip_ver = check_ip_ver(address); + if (ip_ver != 4 && ip_ver != 6) { throw std::invalid_argument("Invalid IP address type"); } @@ -37,11 +38,6 @@ Sock::Sock(int ip_ver, char protocol, const char* address, int port) { this->protocol = protocol; this->port = port; this->address = std::string(address); - - /* Check to see if the given IP address matches the given ip_ver */ - if ((check_ip_ver(address) != 6 && ip_ver == 6) || (check_ip_ver(address) != 4 && ip_ver == 4)) { - throw std::invalid_argument("Invalid IP address for given type."); - } } /* This method sends the given data, through the 'other_sockt' variable.. Client