basic web server
This commit is contained in:
parent
cc7e576a07
commit
068a6e2e70
|
@ -0,0 +1,9 @@
|
|||
#include <stdio.h>
|
||||
|
||||
char *p;
|
||||
int l = 65;
|
||||
|
||||
char *p = (char *) &l;
|
||||
|
||||
|
||||
int q = *(int **) &p = &l;
|
23
structs.c
23
structs.c
|
@ -1,23 +0,0 @@
|
|||
// playing around with structs
|
||||
// win 2024
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
struct abc {
|
||||
int 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;
|
||||
}
|
49
webserver.c
49
webserver.c
|
@ -1,18 +1,29 @@
|
|||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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"
|
||||
"<html>hello, world</html>\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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue