Made 'Client' and 'Server' children of 'Sock' class
parent
2e6b01a9bb
commit
ee0c106236
@ -1,77 +1,27 @@
|
|||||||
#include "easysock.hpp"
|
#include "sock.hpp"
|
||||||
#include <iostream>
|
|
||||||
#include <cerrno>
|
|
||||||
#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.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Client {
|
class Client : public Sock {
|
||||||
private:
|
|
||||||
int ip_ver;
|
|
||||||
char protocol;
|
|
||||||
int port;
|
|
||||||
int sock_fd;
|
|
||||||
std::string address;
|
|
||||||
|
|
||||||
|
private:
|
||||||
void create_socket() {
|
void create_socket() {
|
||||||
struct sockaddr* dest = (struct sockaddr *)malloc(sizeof(struct sockaddr));
|
Sock::create_socket();
|
||||||
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 < 0) {
|
||||||
if (sock_fd * -1 == ECONNREFUSED) {
|
if (sock_fd * -1 == ECONNREFUSED) {
|
||||||
throw EXCEPT_CONNREFUSED;
|
throw EXCEPT_CONNREFUSED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Client() {}
|
Client() {}
|
||||||
|
|
||||||
Client(int ip_ver, char protocol, const char* address, int port) {
|
Client(int ip_ver, char protocol, const char* address, int port) : Sock(ip_ver, protocol, address, port) {}
|
||||||
/* Error checking */
|
|
||||||
if (ip_ver != 4 && ip_ver != 6) {
|
|
||||||
throw std::invalid_argument("Invalid IP address type");
|
|
||||||
}
|
|
||||||
if (port < 1024 || port > 65535) {
|
|
||||||
throw std::invalid_argument("Invalid port");
|
|
||||||
}
|
|
||||||
if (protocol != 'T' && protocol != 'U') {
|
|
||||||
throw std::invalid_argument("Invalid protocol");
|
|
||||||
}
|
|
||||||
|
|
||||||
this->ip_ver = ip_ver;
|
|
||||||
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.");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
create_socket();
|
|
||||||
} catch (int e) {
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void sendAll(std::string to_send) {
|
|
||||||
int str_length = to_send.length();
|
|
||||||
int num_bytes_sent = 0; /* Number of bytes sent in one call to send */
|
|
||||||
int total_bytes_sent = 0; /* Total number of bytes sent */
|
|
||||||
|
|
||||||
while (total_bytes_sent < str_length) {
|
|
||||||
num_bytes_sent = send(this->getSockFD(), to_send.substr(total_bytes_sent).data(), str_length - total_bytes_sent, 0);
|
|
||||||
total_bytes_sent += num_bytes_sent;
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int getSockFD() {
|
|
||||||
return sock_fd;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/* TODO - Add comments to better explain the inheritance and polymorphism going on */
|
||||||
};
|
};
|
||||||
|
@ -1,75 +1,28 @@
|
|||||||
#include "easysock.hpp"
|
#include "sock.hpp"
|
||||||
#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, and creates a listening socket on that address/port.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Server {
|
class Server : public Sock {
|
||||||
private:
|
|
||||||
int ip_ver;
|
|
||||||
char protocol;
|
|
||||||
int port;
|
|
||||||
int sock_fd;
|
|
||||||
std::string address;
|
|
||||||
|
|
||||||
|
private:
|
||||||
void create_socket() {
|
void create_socket() {
|
||||||
struct sockaddr* dest = (struct sockaddr *)malloc(sizeof(struct sockaddr));
|
Sock::create_socket();
|
||||||
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 < 0) {
|
||||||
if (sock_fd * -1 == EADDRNOTAVAIL) {
|
if (sock_fd * -1 == ECONNREFUSED) {
|
||||||
throw EXCEPT_ADDRNOTAVAIL;
|
throw EXCEPT_CONNREFUSED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Server() {}
|
Server() {}
|
||||||
|
|
||||||
Server(int ip_ver, char protocol, const char* address, int port) {
|
Server(int ip_ver, char protocol, const char* address, int port) : Sock(ip_ver, protocol, address, port) {}
|
||||||
/* Error checking */
|
|
||||||
if (ip_ver != 4 && ip_ver != 6) {
|
|
||||||
throw std::invalid_argument("Invalid IP address type");
|
|
||||||
}
|
|
||||||
if (port < 1024 || port > 65535) {
|
|
||||||
throw std::invalid_argument("Invalid port");
|
|
||||||
}
|
|
||||||
if (protocol != 'T' && protocol != 'U') {
|
|
||||||
throw std::invalid_argument("Invalid protocol");
|
|
||||||
}
|
|
||||||
|
|
||||||
this->ip_ver = ip_ver;
|
|
||||||
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.");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
create_socket();
|
|
||||||
} catch (int e) {
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
std::string recvAll() {
|
|
||||||
std::string string = std::string();
|
|
||||||
char* buffer = (char *)malloc(100);
|
|
||||||
while (recv(this->getSockFD(), buffer, 100, 0) != 0) {
|
|
||||||
string.append(std::string(buffer));
|
|
||||||
}
|
|
||||||
|
|
||||||
return string;
|
|
||||||
}
|
|
||||||
|
|
||||||
int getSockFD() {
|
|
||||||
return sock_fd;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/* TODO - Add comments to better explain the inheritance and polymorphism going on */
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue