|
|
|
#include <fcntl.h>
|
|
|
|
#include "includes/client.hpp"
|
|
|
|
#include "includes/exception_consts.hpp"
|
|
|
|
#include "includes/sock.hpp"
|
|
|
|
#include "includes/easysock.hpp"
|
|
|
|
|
|
|
|
/* Destructor - closes any open sockets */
|
|
|
|
Client::~Client() {
|
|
|
|
close(this->other_socket);
|
|
|
|
close(this->sock_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Creates a client socket. This method extends the parent method, and should be
|
|
|
|
called immediately after the constructor. It also tries to connect to the
|
|
|
|
address specified in the constructor, and will throw an exception if that fails.
|
|
|
|
The exception thrown is an integer, that corresponds to the errno returned by the failing
|
|
|
|
function. This enables a client to 'catch' the thrown exception, and print the corresponding
|
|
|
|
error message using strerror().
|
|
|
|
*/
|
|
|
|
|
|
|
|
void Client::create_socket() {
|
|
|
|
Sock::create_socket();
|
|
|
|
this->sock_fd = create_remote(this->ip_ver, this->protocol, this->address.data(), this->port, dest);
|
|
|
|
if (this->sock_fd < 0) {
|
|
|
|
throw (this->sock_fd * -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sends given data to the peer socket. This method is overriden, because a TCP
|
|
|
|
server socket has a 'peer' socket, that it must send to and receive data from. A
|
|
|
|
client socket (TCP or UDP) does not have this requirement. Therefore, while both
|
|
|
|
sendAll methods perform the same actions, they do so using different sockets. */
|
|
|
|
|
|
|
|
void Client::sendAll(std::string to_send) {
|
|
|
|
this->other_socket = this->sock_fd;
|
|
|
|
Sock::sendAll(to_send);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Receives data from peer socket, and returns it. See above for better
|
|
|
|
explanation of why this method is overriden. */
|
|
|
|
|
|
|
|
std::string Client::recvAll() {
|
|
|
|
this->other_socket = this->sock_fd;
|
|
|
|
return Sock::recvAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the type of socket based on the global constants set in sock.hpp */
|
|
|
|
int Client::get_type() {
|
|
|
|
return SOCK_CLIENT;
|
|
|
|
}
|