diff --git a/prak3/src/client.cpp b/prak3/src/client.cpp index 8d82c4f8750fc1b154c3769f38f4efb12850bf85..98de7266cb3a6d63baaea398307404d4d1f98eff 100644 --- a/prak3/src/client.cpp +++ b/prak3/src/client.cpp @@ -9,7 +9,7 @@ pid_t pid = getpid(); std::string generateRandomKey() { - return "key" + std::to_string(rand() % 100); + return "key" + std::to_string(rand() % 10000); } @@ -93,7 +93,8 @@ int main(int argc, char* argv[]) { int sock; struct sockaddr_in server_address; - char* buffer[bufferSize] = {0}; + + char buffer[bufferSize] = {0}; // Socket erstellen sock = socket(AF_INET, SOCK_STREAM, 0); diff --git a/prak3/src/server.cpp b/prak3/src/server.cpp index 909612ef80ad6aad6c193832774f17654f30a1c9..64de8a42b18f7afea26718e199d4762a6a711840 100644 --- a/prak3/src/server.cpp +++ b/prak3/src/server.cpp @@ -46,10 +46,38 @@ struct Request { // Create a new Request struct Request* req = new Request(); + // Copy the input buffer to a mutable string for tokenization + char* input = strdup(buffer); // Duplicate the buffer + if (!input) { + return nullptr; // Handle memory allocation failure + } + + // Tokenize the input string using ',' and ';' as delimiters + char* token = strtok(input, ",;"); + if (token) { + // First token is the operation + req->operation = token[0]; // Extract the first character (s, g, or d) + + // Second token is the key + token = strtok(nullptr, ",;"); + if (token) { + req->key = token; + + // Third token is the value (only for store operation) + if (req->operation == 's') { + token = strtok(nullptr, ",;"); + if (token) { + req->value = token; + } + } + } + } + + // Free the duplicated input string + free(input); return req; } -// we assume that this function is slow std::string processRequest_slow(const std::string& request) { std::string response; @@ -80,7 +108,7 @@ std::string processRequest_slow(const std::string& request) { response = "Invalid operation!"; break; } - + free(parsedRequest); return response; } //============================================= @@ -154,6 +182,7 @@ void handle_client(int client_socket) { send(client_socket, response.c_str(), response.size(), 0); } + close(client_socket); }