Mercurial
diff seobeo/main.c @ 1:adcfad6e86fb
Updated naming and separated out some logic within seobeo.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Wed, 24 Sep 2025 09:11:20 -0700 |
| parents | 5695ef413be0 |
| children | 2758f5527d2b |
line wrap: on
line diff
--- a/seobeo/main.c Tue Sep 23 10:05:25 2025 -0700 +++ b/seobeo/main.c Wed Sep 24 09:11:20 2025 -0700 @@ -2,22 +2,7 @@ ** server.c -- a stream socket server demo */ -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <errno.h> -#include <string.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <netdb.h> -#include <arpa/inet.h> -#include <sys/wait.h> -#include <signal.h> - -#define PORT "6969" - -#define BACKLOG 10 // how many pending connections queue will hold +#include "seobeo/seobeo.h" void SigchildHandler(int s) { @@ -42,12 +27,48 @@ return &(((struct sockaddr_in6*)sa)->sin6_addr); } +void HandleClientRequest(int client_fd) +{ + FILE *file = fopen("seobeo/index.html", "rb"); + if (!file) { + perror("fopen"); + return; + } + + fseek(file, 0, SEEK_END); + size_t size = ftell(file); + fseek(file, 0, SEEK_SET); + + char *data = malloc(size); + fread(data, 1, size, file); + fclose(file); + + char *header = malloc(100); + sprintf( + header, + "HTTP/1.1 200 OK\r\n" + "Content-Type: text/html\r\n" + "Content-Length: %zu\r\n" + "Connection: close\r\n" + "\r\n", + size + ); + + send(client_fd, header, strlen(header), 0); + ssize_t total_sent = 0; + while (total_sent < size) + { + ssize_t sent = send(client_fd, data + total_sent, size - total_sent, 0); + if (sent <= 0) break; + total_sent += sent; + } + + close(client_fd); +} + int main(void) { - int sock_fd, client_fd; - struct addrinfo hints, *server_infos, *free_server_info; - int yes = 1; // boolean - + int32 client_fd; struct sockaddr_storage client_addr; socklen_t sin_size; @@ -55,56 +76,6 @@ struct sigaction sa; - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; - hints.ai_flags = AI_PASSIVE; - - if (getaddrinfo(NULL, PORT, &hints, &server_infos) == 1) - { - perror("getaddrinfo"); - return 1; - } - - for (free_server_info = server_infos; free_server_info != NULL; free_server_info = free_server_info->ai_next) - { - if ((sock_fd = socket(free_server_info->ai_family, free_server_info->ai_socktype, free_server_info->ai_protocol)) == -1) - { - perror("socket"); - continue; - } - - if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) - { - perror("setsockopt"); - continue; - } - - if (bind(sock_fd, free_server_info->ai_addr, free_server_info->ai_addrlen) == -1) - { - close(sock_fd); - perror("setsockopt"); - continue; - } - - // binded to a open server infos; - break; - } - - freeaddrinfo(server_infos); - - if (free_server_info == NULL) - { - perror("No free server"); - return 1; - } - - if (listen(sock_fd, BACKLOG) != 0) - { - perror("listen"); - return 1; - } - sa.sa_handler = SigchildHandler; // reap all dead processes sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; @@ -113,15 +84,9 @@ exit(1); } - printf("server: waiting for connections...\n"); + int sock_fd = Seobeo_CreateSocket(1, "6969", 10); - char *response = - "HTTP/1.1 OK %s\r\n" - "Content-Type: text/txt\r\n" - "Content-Length: 13\r\n" - "Connection: close\r\n" - "\r\n" - "Hello, world!"; + printf("server: waiting for connections...\n"); while (1) { @@ -131,7 +96,7 @@ if (client_fd == -1) { perror("accept"); - continue; + break; } inet_ntop( @@ -145,12 +110,7 @@ if (!fork()) { close(sock_fd); - if (send(client_fd, response, strlen(response), 0) == -1) - { - perror("send"); - continue; - } - close(client_fd); + HandleClientRequest(client_fd); exit(0); }