-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add(client): implement client for server test
- Loading branch information
Showing
4 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
!srcs | ||
!srcs/Socket | ||
!srcs/Server | ||
!srcs/Client | ||
!www | ||
!includes | ||
!test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include <arpa/inet.h> | ||
#include <errno.h> | ||
#include <sys/socket.h> | ||
#include <unistd.h> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <cstring> | ||
#include <iostream> | ||
#include <stdexcept> | ||
#include "Client.hpp" | ||
#include "webserv.hpp" | ||
#include "Color.hpp" | ||
|
||
Client::Client(const char *server_ip, const char *server_port) { | ||
this->_connect_fd = create_connect_socket(); | ||
this->_addr = create_connect_addr(server_ip, server_port); | ||
} | ||
|
||
Client::~Client() { | ||
if (_connect_fd != ERROR) { | ||
errno = 0; | ||
if (close(_connect_fd) == ERROR) { | ||
std::cerr << strerror(errno) << std::endl; | ||
} | ||
_connect_fd = ERROR; | ||
} | ||
} | ||
|
||
int Client::create_connect_socket() { | ||
int connect_fd; | ||
errno = 0; | ||
connect_fd = socket(AF_INET, SOCK_STREAM, 0); | ||
if (connect_fd == ERROR) { | ||
throw std::runtime_error(strerror(errno)); | ||
} | ||
return connect_fd; | ||
} | ||
|
||
struct sockaddr_in Client::create_connect_addr(const char *server_ip, | ||
const char *server_port) { | ||
struct sockaddr_in addr = {}; | ||
const int DECIMAL_BASE = 10; | ||
|
||
addr.sin_family = AF_INET; | ||
addr.sin_port = htons(std::strtol(server_port, NULL, DECIMAL_BASE)); | ||
addr.sin_addr.s_addr = inet_addr(server_ip); | ||
return addr; | ||
} | ||
|
||
void Client::process_server_connect(const std::string &send_msg) { | ||
connect_to_server(this->_connect_fd, this->_addr); | ||
send_to_server(this->_connect_fd, send_msg); | ||
this->_recv_message = recv_message_from_server(this->_connect_fd); | ||
} | ||
|
||
void Client::connect_to_server(int connect_fd, struct sockaddr_in addr) { | ||
socklen_t len = sizeof(addr); | ||
|
||
errno = 0; | ||
if (connect(connect_fd, (struct sockaddr *)&addr, len) == ERROR) { | ||
throw std::runtime_error(strerror(errno)); | ||
} | ||
} | ||
|
||
void Client::send_to_server(int connect_fd, const std::string &send_msg) { | ||
ssize_t send_size; | ||
size_t msg_len = send_msg.size() + 1; | ||
const char *msg = send_msg.c_str(); | ||
|
||
errno = 0; | ||
send_size = send(connect_fd, msg, msg_len, 0); | ||
if (send_size == ERROR) { | ||
throw std::runtime_error(strerror(errno)); | ||
} | ||
} | ||
|
||
std::string Client::recv_message_from_server(int connect_fd) { | ||
ssize_t recv_size; | ||
char buf[BUFSIZ + 1]; | ||
std::string recv_msg; | ||
|
||
while (true) { | ||
errno = 0; | ||
recv_size = recv(connect_fd, &buf, BUFSIZ, 0); | ||
if (recv_size == ERROR) { | ||
throw std::runtime_error(strerror(errno)); | ||
} | ||
buf[recv_size] = '\0'; | ||
recv_msg += buf; | ||
if (recv_size < BUFSIZ) { | ||
break; | ||
} | ||
} | ||
return recv_msg; | ||
} | ||
|
||
std::string Client::get_recv_message() const { return this->_recv_message; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <arpa/inet.h> | ||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
#include <string> | ||
#include "webserv.hpp" | ||
|
||
class Client { | ||
public: | ||
Client(const char *server_ip, const char *server_port); | ||
~Client(); | ||
|
||
std::string get_recv_message() const; | ||
void process_server_connect(const std::string &send_msg); | ||
|
||
private: | ||
int _connect_fd; | ||
std::string _recv_message; | ||
struct sockaddr_in _addr; | ||
|
||
static int create_connect_socket(); | ||
static struct sockaddr_in create_connect_addr(const char *server_ip, const char *server_port); | ||
static void connect_to_server(int connect_fd, struct sockaddr_in addr); | ||
static void send_to_server(int connect_fd, const std::string &send_msg); | ||
static std::string recv_message_from_server(int connect_fd); | ||
}; |