Added address parameter to constructor, and added additional error handling

master
Aadhavan Srinivasan 11 months ago
parent 7c741de587
commit 1359f22f71

@ -1,6 +1,7 @@
#include "easysock.hpp"
#include <iostream>
#include <cerrno>
#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.");
}
try {
create_socket();
} catch (int e) {
throw;
}
}

Loading…
Cancel
Save