|
|
@ -25,7 +25,7 @@ Sock::Sock(int ip_ver, char protocol, const char* address, int port) {
|
|
|
|
if (port < 1024 || port > 65535) {
|
|
|
|
if (port < 1024 || port > 65535) {
|
|
|
|
throw std::invalid_argument("Invalid port");
|
|
|
|
throw std::invalid_argument("Invalid port");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (protocol != 'T' && protocol != 'U') {
|
|
|
|
if (protocol != ES_TCP && protocol != ES_UDP) {
|
|
|
|
throw std::invalid_argument("Invalid protocol");
|
|
|
|
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
|
|
|
|
/* This method sends the given data, through the 'other_sockt' variable.. Client
|
|
|
|
classes extend this method, by calling it with different parameters.
|
|
|
|
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. */
|
|
|
|
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 str_length = to_send.length();
|
|
|
|
int num_bytes_sent = 0; /* Number of bytes sent in one call to send */
|
|
|
|
int num_bytes_sent = 0; /* Number of bytes sent in one call to send */
|
|
|
|
int total_bytes_sent = 0; /* Total number of bytes sent */
|
|
|
|
int total_bytes_sent = 0; /* Total number of bytes sent */
|
|
|
|
|
|
|
|
|
|
|
|
/* For UDP sockets */
|
|
|
|
/* For UDP sockets */
|
|
|
|
if (this->protocol == 'U') {
|
|
|
|
if (this->protocol == ES_UDP) {
|
|
|
|
sendto(this->sock_fd, to_send.data(), str_length, 0, dest, addrlen);
|
|
|
|
sendto(this->sock_fd, to_send.data(), str_length, 0, dest, addrlen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* For TCP sockets */
|
|
|
|
/* For TCP sockets */
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
while (total_bytes_sent < str_length) {
|
|
|
|
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) {
|
|
|
|
if (num_bytes_sent < 0) {
|
|
|
|
throw errno * -1;
|
|
|
|
throw errno * -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -67,24 +68,24 @@ void Sock::sendAll(std::string to_send, int dest_sock) {
|
|
|
|
return;
|
|
|
|
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'
|
|
|
|
'recv' method is called until all the data has been read. For UDP, the 'recvfrom'
|
|
|
|
method is only called once.
|
|
|
|
method is only called once.
|
|
|
|
This function also needs more testing for TCP. */
|
|
|
|
This function also needs more testing for TCP. */
|
|
|
|
|
|
|
|
|
|
|
|
std::string Sock::recvAll(int from_sock) {
|
|
|
|
std::string Sock::recvAll() {
|
|
|
|
int num_bytes_received;
|
|
|
|
int num_bytes_received;
|
|
|
|
std::string string = std::string();
|
|
|
|
std::string string = std::string();
|
|
|
|
char* buffer = (char *)malloc(100);
|
|
|
|
char* buffer = (char *)malloc(100);
|
|
|
|
bool has_been_read = false;
|
|
|
|
bool has_been_read = false;
|
|
|
|
|
|
|
|
|
|
|
|
if (this->protocol == 'U') {
|
|
|
|
if (this->protocol == ES_UDP) {
|
|
|
|
recvfrom(this->sock_fd, buffer, 100, 0, dest, &addrlen);
|
|
|
|
recvfrom(this->sock_fd, buffer, 100, 0, dest, &addrlen);
|
|
|
|
string.append(std::string(buffer));
|
|
|
|
string.append(std::string(buffer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* For TCP sockets */
|
|
|
|
/* For TCP sockets */
|
|
|
|
else {
|
|
|
|
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 ((errno == EAGAIN || errno == EWOULDBLOCK)) {
|
|
|
|
if (has_been_read) {
|
|
|
|
if (has_been_read) {
|
|
|
|