From 3006bfa5e20b478894d5c39d134584446a144390 Mon Sep 17 00:00:00 2001 From: bettercallous Date: Tue, 9 Apr 2024 02:00:23 +0000 Subject: [PATCH] implemented client data processing --- Server.cpp | 23 ++++++++++++++++++++--- Server.hpp | 2 ++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Server.cpp b/Server.cpp index 0f04121..2adddda 100644 --- a/Server.cpp +++ b/Server.cpp @@ -50,8 +50,7 @@ void Server::run() { if (_fds[i].fd == _serverSocketFd) handleClientConnection(); else - // handle existing client incoming data here - continue; + handleClientData(_fds[i].fd); } } } @@ -111,4 +110,22 @@ void Server::handleClientConnection() { std::cout << "Client <" << newFd << "> Connected" << std::endl; } } -} \ No newline at end of file +} + +void Server::handleClientData(int fd) { + char buffer[BUFFER_SIZE] = {0}; + + // Receive the data + ssize_t bytes = recv(fd, buffer, BUFFER_SIZE - 1 , 0); + + // Check if the client disconnected + if (bytes <= 0) { + std::cout << "Client <" << fd << "> Disconnected" << std::endl; + // cleanups here + } else { + buffer[bytes] = '\0'; + std::cout << "Client <" << fd << "> Data: " << buffer; + + // parse, check, and handle the received data here + } +} diff --git a/Server.hpp b/Server.hpp index 2ca173c..4685ea8 100644 --- a/Server.hpp +++ b/Server.hpp @@ -9,6 +9,7 @@ #include #include #include "Client.hpp" +#define BUFFER_SIZE 1024 class Server { private: @@ -31,6 +32,7 @@ class Server { void bindServerSocket(); void addPollfd(int fd, short events, short revents); void handleClientConnection(); + void handleClientData(int fd); }; #endif \ No newline at end of file