add client testing

This commit is contained in:
2024-11-08 22:19:47 +01:00
parent 7f186ff44b
commit f13426283e
10 changed files with 156 additions and 28 deletions

38
src/client.c Normal file
View File

@@ -0,0 +1,38 @@
// THIS FILE IS ONLY A TEST FOR THE BASIC STUFF
#include "client.h"
#include "colors.h"
int test_client_connection(const char *hostname, const char *port) {
struct addrinfo hints;
struct addrinfo *res;
memset(&hints, 0, sizeof hints);
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
int status = getaddrinfo(hostname, port, &hints, &res);
if (status != 0) {
fprintf(stderr, RED BOLD "[client] getaddrinfo(): %s\n" RESET,
gai_strerror(status));
exit(EXIT_FAILURE);
}
int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
status = connect(sockfd, res->ai_addr, res->ai_addrlen);
if (status != 0) {
fprintf(stderr, RED BOLD "[client] connect(): %s\n" RESET,
gai_strerror(status));
exit(EXIT_FAILURE);
}
char buf[4096];
int bytes_read = recv(sockfd, buf, 4096, 0);
fprintf(stdout, BLUE "[client] Read %d bytes: %s\n" RESET, bytes_read,
buf);
freeaddrinfo(res);
return 0;
}

View File

@@ -1,17 +1,14 @@
#include "main.h"
#include "colors.h"
#include "server.h"
#include "utils.h"
int main(int argc, char **argv) {
fprintf(stdout, BOLD GREEN "Running cws...\n" RESET);
fprintf(stdout, BOLD GREEN "[server] Running cws...\n" RESET);
int ret = start_server();
int ret = start_server(NULL, "3030");
if (ret < 0) {
fprintf(stderr, BOLD RED "Unable to start web server\n");
}
print_ips("google.com", "80");
return 0;
}

14
src/mainc.c Normal file
View File

@@ -0,0 +1,14 @@
#include "client.h"
#include "colors.h"
#include "main.h"
int main(int argc, char **argv) {
fprintf(stdout, BOLD GREEN "[client] Running cws...\n" RESET);
int ret = test_client_connection("localhost", "3030");
if (ret < 0) {
fprintf(stderr, BOLD RED "Unable to start client\n");
}
return 0;
}

View File

@@ -1 +1,2 @@
sources = files('main.c', 'server.c', 'utils.c')
server = files('main.c', 'server.c', 'utils.c')
client = files('mainc.c', 'client.c')

View File

@@ -1,34 +1,49 @@
#include "server.h"
#include "colors.h"
int start_server(void) {
char ipv4[INET_ADDRSTRLEN];
struct sockaddr_in sa;
inet_pton(AF_INET, "192.168.0.1", &(sa.sin_addr));
inet_ntop(AF_INET, &(sa.sin_addr), ipv4, INET_ADDRSTRLEN);
fprintf(stdout, BLUE "IPv4: %s\n" RESET, ipv4);
return 0;
}
void get_local_ip(void) {
char service[] = "3030";
int start_server(const char *hostname, const char *service) {
struct addrinfo hints;
struct addrinfo *res;
struct sockaddr_storage their_sa; // incoming clients
socklen_t theirsa_size = sizeof their_sa;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6
hints.ai_socktype = SOCK_STREAM; // TCP
hints.ai_flags = AI_PASSIVE; // fill in IP for me
int status = getaddrinfo(NULL, service, &hints, &res);
int status = getaddrinfo(hostname, service, &hints, &res);
if (status != 0) {
fprintf(stderr, RED "getaddrinfo() error: %s\n" RESET,
fprintf(stderr,
RED BOLD "[server] getaddrinfo() error: %s\n" RESET,
gai_strerror(status));
exit(1);
exit(EXIT_FAILURE);
}
// socket file descriptor
int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
status = bind(sockfd, res->ai_addr, res->ai_addrlen);
if (status != 0) {
fprintf(stderr, RED BOLD "[server] bind(): %s\n" RESET,
gai_strerror(status));
exit(EXIT_FAILURE);
}
status = listen(sockfd, BACKLOG);
if (status != 0) {
fprintf(stderr, RED BOLD "[server] listen(): %s\n" RESET,
gai_strerror(status));
exit(EXIT_FAILURE);
}
char *msg = "Hello there!";
int msg_len = strlen(msg);
int newfd = accept(sockfd, (struct sockaddr *)&their_sa, &theirsa_size);
int bytes_sent = send(newfd, msg, msg_len, 0);
fprintf(stdout, BLUE "[server] Sent %d bytes\n" RESET, bytes_sent);
freeaddrinfo(res);
return 0;
}