Last active
May 3, 2020 08:48
-
-
Save pranavgade20/9e7bbbd08622f03f20910e159d8b849f to your computer and use it in GitHub Desktop.
A very shitty server written in C. Do yourself a favour and do not try to use this.
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
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/socket.h> | |
#include <stdlib.h> | |
#include <netinet/in.h> | |
#include <string.h> | |
#include <errno.h> | |
#define PORT 8080 | |
int main(int argc, char const *argv[]) | |
{ | |
struct sockaddr_in address; | |
int addrlen = sizeof(address); | |
// Creating socket file descriptor | |
int server_socket_fd = socket(AF_INET, SOCK_STREAM, 0); // 0 is for the default protocol | |
if (server_socket_fd == 0) | |
{ | |
perror("Error creating the server socket.\n"); | |
exit(EXIT_FAILURE); | |
} | |
int opt = 1; | |
if (setsockopt(server_socket_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) | |
{ | |
char err[100]; | |
sprintf(err, "Error number: %d\n", errno); | |
perror(err); | |
exit(EXIT_FAILURE); | |
} | |
address.sin_family = AF_INET; | |
address.sin_addr.s_addr = INADDR_ANY; | |
address.sin_port = htons( PORT ); | |
// attaching socket to the port 8080 | |
if (bind(server_socket_fd, (struct sockaddr *)&address, sizeof(address)) < 0) | |
{ | |
perror("bind failed"); | |
exit(EXIT_FAILURE); | |
} | |
if (listen(server_socket_fd, 3) < 0) | |
{ | |
perror("cannot listen"); | |
exit(EXIT_FAILURE); | |
} | |
printf("Listening for connections on port: %d\n", PORT); | |
while(1) | |
{ | |
int client_socket = accept(server_socket_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen); | |
if (client_socket < 0) | |
{ | |
perror("could not accept client socket"); | |
exit(EXIT_FAILURE); | |
} | |
char buffer[1024] = {0}; | |
read(client_socket, buffer, 1024); | |
char * line = strtok(buffer, "\r\n"); | |
strtok(line, " "); | |
char * req_addr = strtok(NULL, " "); | |
printf("Requested: %s\n", req_addr); | |
if (req_addr[0] == '/' && req_addr[1] == '\0') | |
{ | |
char * response = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n<html> <body> Hlo! What is your name? <form method=\"get\" action=\"/name\"><input type=\"text\" name=\"q\"><button type=\"submit\">Submit</button></form></body></html>\r\n\r\n"; // maybe replace with CLRF | |
send(client_socket , response , strlen(response) , 0); | |
} else if (!strncmp("/name", req_addr, strlen("/name"))) | |
{ | |
strtok(req_addr, "="); | |
char * name = strtok(NULL, " "); | |
char * response = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nHello, "; // maybe replace with CLRF | |
send(client_socket , response , strlen(response) , 0); | |
send(client_socket , name , strlen(name) , 0); | |
send(client_socket , "\r\n\r\n", strlen("\r\n\r\n"), 0); | |
} else | |
{ | |
char * response = "HTTP/1.1 404\r\nConnection: close\r\n\r\nErr: requested address not found\r\n\r\n"; // maybe replace with CLRF | |
send(client_socket , response , strlen(response) , 0); | |
} | |
if (close(client_socket)) | |
{ | |
char err[100]; | |
sprintf(err, "Error number: %d\n", errno); | |
perror(err); | |
} | |
printf("Response message sent\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment