This code implements a simple key-value store server that can handle SET, GET, and DELETE operations through a client-server architecture using sockets. The server is multithreaded, allowing it to handle multiple client connections simultaneously.
- /server -> server program related sources
- /client -> client program related sources
- /common -> common inclusions
- /util -> common utilization
- /build -> target build directory
Code out your /server/server.c and /client/client.c, then
$ makeTest your /build/server and build/client.
Key-Value Storage:
- The key-value pairs are stored in a linked list structure. There are 26 possible linked lists (one for each letter of the alphabet) to handle different keys.
- Each linked list node (
KeyValue) holds a key and a pointer to aValuestructure that contains the associated value.
1.Server Functions:
open_serverfd(char *port): This function sets up the server to listen on the specified port. It creates a socket, binds it to the port, and starts listening for incoming connections.response(void *socket_desc): This function is the entry point for a new thread handling a client request. It reads commands from the client, processes them, and sends the appropriate response back.processing(char operation[], char firstLetter, char key[], char *value): This function determines which operation (SET, GET, DELETE) the client requested and calls the appropriate processing function.
-
Client Functions:
open_clientfd(char *hostname, char *port): This function connects the client to the server at the specified hostname and port. It handles reading and writing data to the server.- The client interacts with the server by sending commands like
SET key value,GET key, orDELETE key.
-
Command Processing:
- SET Operation:
setProcessingadds a new key-value pair to the linked list or returns an error if the key already exists. - GET Operation:
getProcessingsearches for a key in the linked list and retrieves its value. - DELETE Operation:
deleteProcessingremoves a key-value pair from the linked list.
- SET Operation:
-
Multithreading:
- The server uses the
pthread_createfunction to create a new thread for each client connection. This allows the server to handle multiple clients concurrently.
- The server uses the
-
Error Handling:
- The server and client functions include basic error handling, such as checking if a socket was created successfully or if the connection to the server was successful.
-
Command Parsing:
- The client and server use
strtokto parse incoming command strings, splitting them into command, key, and value components.
- The client and server use
This code demonstrates a basic client-server model using sockets to implement a simple key-value store. The server can handle multiple clients concurrently using threads and supports basic operations like setting, getting, and deleting key-value pairs. The implementation includes error handling and command parsing to ensure smooth operation.