implemented client data processing

This commit is contained in:
bettercallous
2024-04-09 02:00:23 +00:00
parent 4c1b396154
commit 3006bfa5e2
2 changed files with 22 additions and 3 deletions

View File

@@ -50,8 +50,7 @@ void Server::run() {
if (_fds[i].fd == _serverSocketFd) if (_fds[i].fd == _serverSocketFd)
handleClientConnection(); handleClientConnection();
else else
// handle existing client incoming data here handleClientData(_fds[i].fd);
continue;
} }
} }
} }
@@ -111,4 +110,22 @@ void Server::handleClientConnection() {
std::cout << "Client <" << newFd << "> Connected" << std::endl; std::cout << "Client <" << newFd << "> Connected" << std::endl;
} }
} }
} }
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
}
}

View File

@@ -9,6 +9,7 @@
#include <vector> #include <vector>
#include <poll.h> #include <poll.h>
#include "Client.hpp" #include "Client.hpp"
#define BUFFER_SIZE 1024
class Server { class Server {
private: private:
@@ -31,6 +32,7 @@ class Server {
void bindServerSocket(); void bindServerSocket();
void addPollfd(int fd, short events, short revents); void addPollfd(int fd, short events, short revents);
void handleClientConnection(); void handleClientConnection();
void handleClientData(int fd);
}; };
#endif #endif