Added address parameter to constructor, and added additional error handling
This commit is contained in:
@@ -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.");
|
||||
}
|
||||
|
||||
create_socket();
|
||||
try {
|
||||
create_socket();
|
||||
} catch (int e) {
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user