Added address parameter to constructor, and added additional error handling
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#include "easysock.hpp"
|
#include "easysock.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <cerrno>
|
||||||
|
#include "exception_consts.hpp"
|
||||||
/*
|
/*
|
||||||
Server class - Defines a TCP/UDP server.
|
Server class - Defines a TCP/UDP server.
|
||||||
- The constructor takes in a **local** address and port, on which the server listens.
|
- The constructor takes in a **local** address and port, on which the server listens.
|
||||||
@@ -17,12 +18,17 @@ private:
|
|||||||
void create_socket() {
|
void create_socket() {
|
||||||
struct sockaddr* dest = (struct sockaddr *)malloc(sizeof(struct sockaddr));
|
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);
|
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:
|
public:
|
||||||
Server() {}
|
Server() {}
|
||||||
|
|
||||||
Server(int ip_ver, char protocol, int port) {
|
Server(int ip_ver, char protocol, const char* address, int port) {
|
||||||
/* Error checking */
|
/* Error checking */
|
||||||
if (ip_ver != 4 && ip_ver != 6) {
|
if (ip_ver != 4 && ip_ver != 6) {
|
||||||
throw std::invalid_argument("Invalid IP address type");
|
throw std::invalid_argument("Invalid IP address type");
|
||||||
@@ -37,13 +43,18 @@ public:
|
|||||||
this->ip_ver = ip_ver;
|
this->ip_ver = ip_ver;
|
||||||
this->protocol = protocol;
|
this->protocol = protocol;
|
||||||
this->port = port;
|
this->port = port;
|
||||||
if (ip_ver == 4) {
|
this->address = std::string(address);
|
||||||
this->address = "127.0.0.1";
|
|
||||||
} else if (ip_ver == 6) {
|
/* Check to see if the given IP address matches the given ip_ver */
|
||||||
this->address = "::1";
|
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