Created
June 30, 2022 17:08
-
-
Save epshteinmatthew/df66de8fc0e3c10f830130d57e022cd7 to your computer and use it in GitHub Desktop.
Simple C Web Server from "scratch"
This file contains hidden or 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 <arpa/inet.h> | |
| #include <errno.h> | |
| #include <stdio.h> | |
| #include <sys/socket.h> | |
| #include <sys/types.h> | |
| #include <unistd.h> | |
| #include <string.h> | |
| //gcc -Wall hello.c -o webserver | |
| //./webserver | |
| #define PORT 6969 | |
| #define BUFFER_SIZE 1024 | |
| int main(int argc, char *argv[]) { | |
| char buffer[BUFFER_SIZE]; | |
| char resp[] = "HTTP/1.0 200 OK\r\n" | |
| "Server: webserver-c\r\n" | |
| "Content-type: text/html\r\n\r\n" | |
| "<html>hello, world</html>\r\n"; | |
| int sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
| if (sockfd == -1) | |
| { | |
| perror("gobble gobble fuck"); | |
| return 1; | |
| } | |
| printf("socket created \n"); | |
| //creating socket address vars | |
| struct sockaddr_in host_addr; | |
| int host_addrlen = sizeof(host_addr); | |
| host_addr.sin_family = AF_INET; | |
| host_addr.sin_port = htons(PORT); | |
| host_addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
| struct sockaddr_in client_addr; | |
| int client_addrlen = sizeof(client_addr); | |
| if(bind(sockfd, (struct sockaddr *)&host_addr, host_addrlen) != 0){ | |
| perror("binda bonda we'va failda"); | |
| return 1; | |
| } | |
| printf("socket bound\n"); | |
| if(listen(sockfd, SOMAXCONN) != 0){ | |
| perror("lista bista this is like windows vista"); | |
| return 1; | |
| } | |
| printf("socket set to passive, incomeing connections ready to accept\n"); | |
| for(;;){ | |
| int acceptedfd = accept(sockfd, (struct sockaddr *)&host_addr, (socklen_t *)&host_addrlen); | |
| if (acceptedfd < 0) | |
| { | |
| perror("Accepta adepta time to chrash-da"); | |
| continue; | |
| } | |
| printf("accepted connection\n"); | |
| // Get client address | |
| int sockn = getsockname(acceptedfd, (struct sockaddr *)&client_addr, | |
| (socklen_t *)&client_addrlen); | |
| if (sockn < 0) { | |
| perror("webserver (getsockname)"); | |
| continue; | |
| } | |
| int valread = read(acceptedfd, buffer, BUFFER_SIZE); | |
| if (valread < 0){ | |
| perror("reada beada we have a steada"); | |
| continue; | |
| } | |
| char method[BUFFER_SIZE], uri[BUFFER_SIZE], version[BUFFER_SIZE]; | |
| sscanf(buffer, "%s %s %s", method, uri, version); | |
| if (strcmp(uri, "/") == 0) | |
| { | |
| printf("noice"); | |
| } | |
| printf("[%s:%u] %s %s %s\n", inet_ntoa(client_addr.sin_addr), | |
| ntohs(client_addr.sin_port), method, version, uri); | |
| int valwrite = write(acceptedfd, resp, strlen(resp)); | |
| if (valwrite < 0) { | |
| perror("write vhite this server aint too bright"); | |
| continue; | |
| } | |
| close(acceptedfd); | |
| } | |
| printf("Hello World \n"); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment