From 068a6e2e706681260b97223c33c97712ba1c800b Mon Sep 17 00:00:00 2001 From: winsdominoes Date: Wed, 15 May 2024 14:52:38 +0700 Subject: [PATCH] basic web server --- goofy.c | 9 +++++++++ structs | Bin 16008 -> 0 bytes structs.c | 23 ----------------------- webserver.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 56 insertions(+), 25 deletions(-) create mode 100644 goofy.c delete mode 100755 structs delete mode 100644 structs.c diff --git a/goofy.c b/goofy.c new file mode 100644 index 0000000..b9558a1 --- /dev/null +++ b/goofy.c @@ -0,0 +1,9 @@ +#include + +char *p; +int l = 65; + +char *p = (char *) &l; + + +int q = *(int **) &p = &l; diff --git a/structs b/structs deleted file mode 100755 index 5057a9e7248cfa826190dd60bb7c64f9cceb3c4a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16008 zcmeHOYit}>6~4Q9Ic*Xr{#m;>TQR_T2NG^WA&r%$?agyXQWi*t07di71pg>eGs~u?B<0#c+O`R7k|tcD0y3 z*QvE?KIwBcX6$hz$ZE>v=uBBB_#BeVTgjCg@?!>8kQgCL=FL`K;sH^CRB|57o5u#x zgR)J=2?=|(Ka0s!Ed5!PDR!U&>F4oKCL6U`O}5RHN2R|Zk>Ih)yt44h!h^ga{Xrt0 z91}jaOFVv%fTyN>R$RgAiOb4H=}~G)xZi8{e6~ra%sim z?gE#&NBW!XIvkMrLrQqn=1jV8TkGabsxgz!l|~vzwr^|P)*373V_S5`JT8k4o>RN` z9a7XxnK22YijEl_?XhjS*x7%zdcBewKfC_9#M9#oe|vlXix)neVjmtibJ&Lqw}&ah zI@cG_!9FG)FHp^3EHAmg&$67YEF%LS!Qv8N1wqoepIZJcv*0`$i;iG9MSv9qN#8)n z`Wm=$oT8WPKj!og9dibf=?npMN%|QrcuqE%&Jof-l5_^rxnw4N+*QMcbj}-4?8Vn< zo7%mnyQ9F(X@q}+l#m@ay5p?7a*CZBVAlYJSN8XwH&bJCq- zTx&Y=HHpx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x; - int y; - int z; -}; - -int main() { - struct abc a = {0, 1, 2}; - struct abc *ptr = &a; - - printf("Address of struct a: %p\n", ptr); - - printf("Address of x in struct a: %p\n", &a.x); - printf("Address of y in struct a: %p\n", &a.y); - printf("Address of z in struct a: %p:\n", &a.z); - - return 0; -} diff --git a/webserver.c b/webserver.c index 4503532..9fcd8db 100644 --- a/webserver.c +++ b/webserver.c @@ -1,18 +1,29 @@ #include #include #include +#include #include +#include #define PORT 8080 +#define BUFFER_SIZE 1024 int main() { + char buffer[BUFFER_SIZE]; + char resp[] = "HTTP/1.0 200\r\n" + "Server: webserver-c\r\n" + "Content-type: text/html\r\n\r\n" + "hello, world\r\n"; + + // create a socket int sockfd = socket(AF_INET, SOCK_STREAM, 0); - if(sockfd == -1) { + if (sockfd == -1) { perror("webserve (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); @@ -20,11 +31,45 @@ int main() { host_addr.sin_port = htons(PORT); host_addr.sin_addr.s_addr = htonl(INADDR_ANY); - if(bind(sockfd, (struct sockaddr *)&host_addr, host_addrlen) != 0) { + // 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 to 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"); + + // read from the socket + int valread = read(newsockfd, buffer, BUFFER_SIZE); + if (valread < 0) { + perror("webserver (read)"); + continue; + } + + // write to the socket + int valwrite = write(newsockfd, resp, strlen(resp)); + if (valwrite < 0) { + perror("webserver (write)"); + continue; + } + + close(newsockfd); + } return 0; }