Skip to content

Commit

Permalink
basic TCP server on c
Browse files Browse the repository at this point in the history
  • Loading branch information
suyash-thakur committed Dec 9, 2023
0 parents commit a511c71
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
6 changes: 6 additions & 0 deletions README.md
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.
83 changes: 83 additions & 0 deletions app/server.c
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;
}
6 changes: 6 additions & 0 deletions spawn_redis_server.sh
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 "$@"

0 comments on commit a511c71

Please sign in to comment.