-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a511c71
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Redis Implementation in C | ||
|
||
|
||
|
||
**Note**: If you're viewing this repo on GitHub, head over to | ||
[codecrafters.io](https://codecrafters.io) to try the challenge. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
#include <netinet/ip.h> | ||
#include <string.h> | ||
#include <errno.h> | ||
#include <unistd.h> | ||
|
||
int main() { | ||
// Disable output buffering | ||
setbuf(stdout, NULL); | ||
|
||
// You can use print statements as follows for debugging, they'll be visible when running tests. | ||
printf("Logs from your program will appear here!\n"); | ||
|
||
// Uncomment this block to pass the first stage | ||
// | ||
int server_fd, client_addr_len; | ||
struct sockaddr_in client_addr; | ||
|
||
server_fd = socket(AF_INET, SOCK_STREAM, 0); | ||
if (server_fd == -1) | ||
{ | ||
printf("Socket creation failed: %s...\n", strerror(errno)); | ||
return 1; | ||
} | ||
|
||
// Since the tester restarts your program quite often, setting REUSE_PORT | ||
// ensures that we don't run into 'Address already in use' errors | ||
int reuse = 1; | ||
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(reuse)) < 0) | ||
{ | ||
printf("SO_REUSEPORT failed: %s \n", strerror(errno)); | ||
return 1; | ||
} | ||
|
||
struct sockaddr_in serv_addr = { | ||
.sin_family = AF_INET, | ||
.sin_port = htons(3000), | ||
.sin_addr = {htonl(INADDR_ANY)}, | ||
}; | ||
|
||
if (bind(server_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != 0) | ||
{ | ||
printf("Bind failed: %s \n", strerror(errno)); | ||
return 1; | ||
} | ||
|
||
int connection_backlog = 5; | ||
if (listen(server_fd, connection_backlog) != 0) | ||
{ | ||
printf("Listen failed: %s \n", strerror(errno)); | ||
return 1; | ||
} | ||
|
||
printf("Waiting for a client to connect...\n"); | ||
client_addr_len = sizeof(client_addr); | ||
|
||
int conn = accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_len); | ||
printf("Client connected\n"); | ||
|
||
while (conn != -1) | ||
{ | ||
// Read the request | ||
char request[1024]; | ||
read(conn, request, 1024); | ||
|
||
printf("Request: %s\n", request); | ||
|
||
// Send the response | ||
char response[1024] = "+PONG\r\n"; | ||
write(conn, response, strlen(response)); | ||
} | ||
|
||
printf("Client disconnected\n"); | ||
close(conn); | ||
|
||
printf("Waiting for a client to connect...\n"); | ||
conn = accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_len); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/sh | ||
|
||
set -e | ||
tmpFile=$(mktemp) | ||
gcc app/*.c -o $tmpFile | ||
exec $tmpFile "$@" |