Added server request handeling

This commit is contained in:
Afonso Clerigo Mendes de Sousa 2025-02-26 22:55:20 +00:00
parent 2321f67457
commit 33fda52f2d

View File

@ -4,12 +4,15 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include "herror.h"
#include "dynmem.h"
#include "ui/ui.h"
#define SERVER_IP "192.168.1.120"
#define PASSWORDS "./files/KEYS.bin"
int main(void)
{
@ -55,6 +58,34 @@ int main(void)
printf("Client connected\n");
// Listen to the client requests
while (1)
{
char buffer[1024] = {0};
int bytes_read = read(connfd, buffer, sizeof(buffer));
if (bytes_read <= 0)
{
fprintf(stderr, "Client disconnected\n");
break;
}
printf("Received request: %s\n", buffer);
// Respond with the list of passwords
FILE *file = fopen(PASSWORDS, "r");
if (file == NULL)
{
fprintf(stderr, "Could not open password file\n");
break;
}
char passwords[1024] = {0};
fread(passwords, sizeof(char), sizeof(passwords), file);
fclose(file);
write(connfd, passwords, strlen(passwords));
}
// Close the socket
close(connfd);
close(sockfd);