Skip to content

Instantly share code, notes, and snippets.

@yashraj1120
Created March 26, 2023 19:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yashraj1120/1a9bace9f63d7b974dc5752e1de38d4c to your computer and use it in GitHub Desktop.
Save yashraj1120/1a9bace9f63d7b974dc5752e1de38d4c to your computer and use it in GitHub Desktop.
simple response server
// webserver.c
// https://bruinsslot.jp/post/simple-http-webserver-in-c/
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#include <signal.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main() {
// sigaction(SIGPIPE, &(struct sigaction){SIG_IGN}, NULL);
signal(SIGPIPE, SIG_IGN);
char buffer[BUFFER_SIZE];
// Create a socket
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("webserver (socket)");
return 1;
}
printf("socket created successfully\n");
// Create the address to bind the socket to
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);
// Create client address
struct sockaddr_in client_addr;
int client_addrlen = sizeof(client_addr);
// Bind the socket to the address
if (bind(sockfd, (struct sockaddr*)&host_addr, host_addrlen) != 0) {
perror("webserver (bind)");
return 1;
}
printf("socket successfully bound to address\n");
// Listen for incoming connections
if (listen(sockfd, SOMAXCONN) != 0) {
perror("webserver (listen)");
return 1;
}
printf("server listening for connections\n");
for (;;) {
// Accept incoming connections
int newsockfd =
accept(sockfd, (struct sockaddr*)&host_addr, (socklen_t*)&host_addrlen);
if (newsockfd < 0) {
perror("webserver (accept)");
continue;
}
printf("connection accepted\n");
// Get client address
int sockn = getsockname(newsockfd, (struct sockaddr*)&client_addr,
(socklen_t*)&client_addrlen);
if (sockn < 0) {
perror("webserver (getsockname)");
continue;
}
// Read from the socket
int valread = read(newsockfd, buffer, BUFFER_SIZE);
if (valread < 0) {
perror("webserver (read)");
continue;
}
// Read the request
char method[BUFFER_SIZE], uri[BUFFER_SIZE], version[BUFFER_SIZE];
sscanf(buffer, "%s %s %s", method, uri, version);
printf("[%s:%u] %s %s %s\n", inet_ntoa(client_addr.sin_addr),
ntohs(client_addr.sin_port), method, version, uri);
/*
if (i == 0) {
char resp[] = "HTTP/1.1 200 OK\r\n"
//"Server: webserver-c\r\n"
// "Cache-Control: no-store\r\n"
"Access-Control-Allow-Origin: *\r\n"
// "Content-type: text/event-stream\r\n"
"Access-Control-Allow-Headers: cache-control\r\n"
"Access-Control-Allow-Private-Network: true\r\n"
"Access-Control-Allow-Credentials: true\r\n\r\n";
// "data: [1,2,3]\n\n";
// Write to the socket
int valwrite1 = write(newsockfd, resp, strlen(resp));
if (valwrite1 < 0) {
perror("webserver (write)");
continue;
}
++i;
}
*/
char response[] =
"HTTP/1.1 200 OK\r\n"
"Server: webserver-c\r\n"
"Cache-Control: no-store\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Content-type: application/octet-stream\r\n"
"Access-Control-Allow-Private-Network: true\r\n\r\n";
// "[1,2,3]";
// Write to the socket
int valwrite = write(newsockfd, response, strlen(response));
if (valwrite < 0) {
perror("webserver (write)");
continue;
}
uint8_t buffer[1764]; // 441 * 4
char *output = malloc((1764 * 4) + 3);
FILE *pipe = popen("parec -d @DEFAULT_MONITOR@", "r");
//free(message);
while (1) {
size_t count = fread(buffer, 1, sizeof(buffer), pipe);
output[0] = '[';
output[1] = 0;
for (size_t i = 0; i < count; i++) {
char data[5];
sprintf(data, "%d", buffer[i]);
strcat(output, data);
if (i < count - 1) {
strcat(output, ",");
}
}
strcat(output, "]");
int valwrite1 = write(newsockfd, output, strlen(output));
printf("write len : %d\n",valwrite1);
if (valwrite1 < 0) {
printf("webserver (write) failed ");
continue;
}
}
/*
for (int i = 0; i < 60; ++i) {
// sleep(1);
char str[2];
sprintf(str, "%d", i);
write(newsockfd, str, strlen(str));
printf("Server UPDATE %d sent.\n", i);
}
*/
close(newsockfd);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment