Added default constructors; [for client] added code to throw an exception if socket creation was unsuccessful

master
Aadhavan Srinivasan 11 months ago
parent 2b1c217dbb
commit 95dea026d9

@ -1,6 +1,7 @@
#include "easysock.hpp" #include "easysock.hpp"
#include <iostream> #include <iostream>
#include <errno.h>
#include "exception_consts.hpp"
/* /*
Client class - Defines a TCP/UDP client. Client class - Defines a TCP/UDP client.
- The constructor takes in a **remote** address and port, and connects the client to that address/port. - The constructor takes in a **remote** address and port, and connects the client to that address/port.
@ -17,9 +18,16 @@ 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_remote(this->ip_ver, this->protocol, this->address.data(), this->port, dest); this->sock_fd = create_remote(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:
Client() {}
Client(int ip_ver, char protocol, const char* address, int port) { Client(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) {
@ -36,7 +44,11 @@ public:
this->protocol = protocol; this->protocol = protocol;
this->port = port; this->port = port;
this->address = std::string(address); this->address = std::string(address);
create_socket(); try {
create_socket();
} catch (int e) {
throw;
}
} }
void sendAll(std::string to_send) { void sendAll(std::string to_send) {

@ -20,6 +20,8 @@ private:
} }
public: public:
Server() {}
Server(int ip_ver, char protocol, int port) { Server(int ip_ver, char protocol, int port) {
/* Error checking */ /* Error checking */
if (ip_ver != 4 && ip_ver != 6) { if (ip_ver != 4 && ip_ver != 6) {

Loading…
Cancel
Save