Build your own Redis
The Guide below is a sequence of tasks which will be divided into various levels and ultimately will help you build your own REDIS
The provided code is a basic TCP server implemented in C.
Let's break down each part to understand its functionality, especially for someone new to socket programming.
Header files
#include <stdio.h>
: Provides functions for input and output operations, such asprintf
for displaying text.#include <stdlib.h>
: Offers general utilities, including memory allocation (malloc
,free
) and process control (exit
).#include <sys/socket.h>
: Contains definitions for socket functions and structures, likesocket
,bind
, andaccept
.#include <netinet/in.h>
: Defines structures for internet operations, such assockaddr_in
, which is used for handling IP addresses.#include <netinet/ip.h>
: Provides declarations for the IP header structure.#include <string.h>
: Offers functions for manipulating C strings and memory, likememset
andstrerror
.#include <errno.h>
: Defines macros for reporting and retrieving error conditions through theerrno
variable.#include <unistd.h>
: Declares standard symbolic constants and types, and declares miscellaneous functions, such asclose
for closing file descriptors.
2. Main Function:
int main() {
: The entry point of the program.setbuf(stdout, NULL);
andsetbuf(stderr, NULL);
: These functions disable buffering for standard output (stdout
) and standard error (stderr
). This means that any output to the console is displayed immediately, which is helpful for debugging purposes.
3. Debugging Message:
printf
: Outputs the specified string to the console. This line informs the user that the program has started and is ready to log messages.
4. Variable Declarations:
int server_fd, client_addr_len;
: Declares two integer variables.server_fd
will store the file descriptor for the server socket, andclient_addr_len
will hold the size of the client's address structure.struct sockaddr_in client_addr;
: Declares a structure to store the client's address information.sockaddr_in
is specifically designed for handling internet addresses.
5. Socket Creation:
server_fd = socket(AF_INET, SOCK_STREAM, 0);
: Creates a new socket.AF_INET
: Specifies the address family for IPv4.SOCK_STREAM
: Indicates that the socket type is stream-oriented, which is used for TCP connections.0
: Specifies the protocol. Setting it to0
lets the system choose the appropriate protocol based on the socket type and address family.
if (server_fd == -1) { ... }
: Checks if the socket creation failed. Ifsocket
returns-1
, an error occurred.strerror(errno)
: Converts the error number (errno
) into a human-readable string describing the error.return 1;
: Exits the program with a status code of1
, indicating an error.
6. Set Socket Options:
int reuse = 1;
: Initializes an integer variablereuse
to1
.setsockopt
: Sets options on the socket.server_fd
: The socket file descriptor.SOL_SOCKET
: Specifies that the option is at the socket level.SO_REUSEADDR
: Allows the socket to bind to an address that is in aTIME_WAIT
state, without waiting for its natural timeout to expire. This is useful for server applications that need to restart and bind to the same port.&reuse
: A pointer to the value of the option.sizeof(reuse)
: The size of the option value.
if (setsockopt(...) < 0) { ... }
: Checks if setting the socket option failed. Ifsetsockopt
returns-1
, an error occurred.
7. Define Server Address:
struct sockaddr_in serv_addr = { ... };
: Initializes asockaddr_in
structure to specify the server's address..sin_family = AF_INET
: Sets the address family to IPv4..sin_port = htons(6379)
: Sets the port number to6379
.htons
(host to network short) converts the port number from host byte order to network byte order, ensuring compatibility across different systems..sin_addr = { htonl(INADDR_ANY) }
: Sets the IP address to accept connections from any interface.htonl
(host to network long) converts the address to network byte order.INADDR_ANY
is a constant that allows the server to accept connections on any of the host
Last updated