diff --git a/client.cpp b/client.cpp index a7cf1bb..c4de08d 100644 --- a/client.cpp +++ b/client.cpp @@ -22,7 +22,7 @@ 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 strerror(this->sock_fd * -1); + throw (this->sock_fd * -1); } } @@ -32,12 +32,19 @@ 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) { - Sock::sendAll(to_send, this->sock_fd); + 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() { - return Sock::recvAll(this->sock_fd); + 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; } diff --git a/server.cpp b/server.cpp index f14b804..b0c4e59 100644 --- a/server.cpp +++ b/server.cpp @@ -4,6 +4,7 @@ #include "includes/server.hpp" #include "includes/exception_consts.hpp" #include "includes/easysock.hpp" +#include "includes/connect_code.hpp" /* Destructor - closes any open sockets */ Server::~Server() { @@ -16,22 +17,37 @@ base method, because a different socket must be used. In the server's case, the 'peer' socket i.e. the socket returned after calling 'accept', must be used. */ void Server::sendAll(std::string to_send) { - Sock::sendAll(to_send, this->other_socket); + Sock::sendAll(to_send); } /* Receives data from socket, and returns it. This function works differently -based on the Transport layer protocol used. For TCP, it calls the parent method -with the peer socket. As mentioned above, this is the socket returned after -calling the accept function. With UDP, there is no concept of a peer socket, and -so the regular server socket (the one created in create_socket() ) is used -instead. */ +based on the Transport layer protocol used. For TCP, it sets the 'other_socket' variable (used +by the parent function) to the peer socket. As mentioned above, this is the +socket returned after calling the accept function. With UDP, there is no concept +of a peer socket, and so the regular server socket (the one created in create_socket()) +is used instead. This function also sets the 'peer_addr' string to the address of the +peer socket, handling both TCP and UDP. */ std::string Server::recvAll() { - if (this->protocol == 'T') { - return Sock::recvAll(this->other_socket); + if (this->protocol == ES_UDP) { + this->other_socket = this->sock_fd; + } + + /* Call receive method of parent */ + std::string to_return = Sock::recvAll(); + + /* Set the peer address of the socket */ + if (this->ip_ver == 4) { + /* FOR IPv4 */ + struct sockaddr_in* temp_struct = (struct sockaddr_in*)this->dest; + /* Convert the s_addr field of the caseted struct to host network-byte, and convert it to a dotted decimal */ + peer_addr = connect_code::dec_to_dotted_dec(std::to_string(htonl(temp_struct->sin_addr.s_addr))); } else { - return Sock::recvAll(this->sock_fd); + /* FOR IPv6 */ + peer_addr = "IPV6 NOT SUPPORTED YET"; } + + return to_return; } /* FOR TCP ONLY - Waits for a peer to connect to the server socket. It stores @@ -40,8 +56,8 @@ nothing if your server socket is a UDP socket. If the accept function fails, the is thrown as an exception. */ void Server::wait_for_peer() { - if (this->protocol == 'T') { - this->other_socket = accept(this->sock_fd, NULL, NULL); + if (this->protocol == ES_TCP) { + this->other_socket = accept(this->sock_fd, dest, &addrlen); if (this->other_socket < 0) { throw errno; } @@ -61,9 +77,17 @@ void Server::create_socket() { if (this->sock_fd < 0) { throw (this->sock_fd * -1); } - if (protocol == 'T') { + if (protocol == ES_TCP) { listen(sock_fd, 10); } } +/* Returns the address of the peer socket as a string, can be used for debugging */ +std::string Server::get_peer_addr() { + return this->peer_addr; +} +/* Returns the type of socket based on the global constants set in sock.hpp */ +int Server::get_type() { + return SOCK_SERVER; +} diff --git a/sock.cpp b/sock.cpp index 9ca0ef8..aa2cc04 100644 --- a/sock.cpp +++ b/sock.cpp @@ -25,7 +25,7 @@ Sock::Sock(int ip_ver, char protocol, const char* address, int port) { if (port < 1024 || port > 65535) { throw std::invalid_argument("Invalid port"); } - if (protocol != 'T' && protocol != 'U') { + if (protocol != ES_TCP && protocol != ES_UDP) { throw std::invalid_argument("Invalid protocol"); } @@ -40,23 +40,24 @@ Sock::Sock(int ip_ver, char protocol, const char* address, int port) { } } -/* This method sends the given data, through the given socket. Client and server -classes extend this method, by calling it with different parameters. +/* This method sends the given data, through the 'other_sockt' variable.. Client +and server classes extend this method, by setting this variable to different values. This function needs more testing for TCP, as it focuses on UDP right now. */ -void Sock::sendAll(std::string to_send, int dest_sock) { +void Sock::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 */ /* For UDP sockets */ - if (this->protocol == 'U') { + if (this->protocol == ES_UDP) { sendto(this->sock_fd, to_send.data(), str_length, 0, dest, addrlen); } /* For TCP sockets */ else { while (total_bytes_sent < str_length) { - num_bytes_sent = send(dest_sock, to_send.substr(total_bytes_sent).data(), str_length - total_bytes_sent, 0); + /* Send the data to the 'other_socket' variable, which should be set by the client and server methods */ + num_bytes_sent = send(this->other_socket, to_send.substr(total_bytes_sent).data(), str_length - total_bytes_sent, 0); if (num_bytes_sent < 0) { throw errno * -1; } @@ -67,24 +68,24 @@ void Sock::sendAll(std::string to_send, int dest_sock) { return; } -/* Receives data from given socket into a string, and returns that string. For TCP, the +/* Receives data from 'other_socket' into a string, and returns that string. For TCP, the 'recv' method is called until all the data has been read. For UDP, the 'recvfrom' method is only called once. This function also needs more testing for TCP. */ -std::string Sock::recvAll(int from_sock) { +std::string Sock::recvAll() { int num_bytes_received; std::string string = std::string(); char* buffer = (char *)malloc(100); bool has_been_read = false; - if (this->protocol == 'U') { + if (this->protocol == ES_UDP) { recvfrom(this->sock_fd, buffer, 100, 0, dest, &addrlen); string.append(std::string(buffer)); } /* For TCP sockets */ else { - while ((num_bytes_received = recv(from_sock, buffer, 100, 0)) != 0) { + while ((num_bytes_received = recv(this->other_socket, buffer, 100, 0)) != 0) { if ((errno == EAGAIN || errno == EWOULDBLOCK)) { if (has_been_read) {