mirror of
https://github.com/opus-tango/http-server-in-c.git
synced 2026-03-20 03:55:25 +00:00
add client handler file and code
This commit is contained in:
7
Makefile
7
Makefile
@@ -1,8 +1,11 @@
|
|||||||
all: webserver.o
|
all: webserver.o client_handler.o
|
||||||
gcc -o webserver.out webserver.o
|
gcc -o webserver.out webserver.o client_handler.o -lpthread
|
||||||
|
|
||||||
webserver: webserver.c
|
webserver: webserver.c
|
||||||
gcc -c webserver webserver.c
|
gcc -c webserver webserver.c
|
||||||
|
|
||||||
|
client_handler: client_handler.c
|
||||||
|
gcc -c client_handler client_handler.c
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *.o *.out
|
rm -f *.o *.out
|
||||||
16
client_handler.c
Normal file
16
client_handler.c
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#include "client_handler.h"
|
||||||
|
|
||||||
|
#define BUFFER_SIZE 1024
|
||||||
|
|
||||||
|
void* client_handler(void* args) {
|
||||||
|
int client = *((int*)args);
|
||||||
|
char* buffer = (char*)malloc(BUFFER_SIZE);
|
||||||
|
|
||||||
|
ssize_t bytes_read = recv(client, buffer, BUFFER_SIZE, 0);
|
||||||
|
if (bytes_read > 0) {
|
||||||
|
buffer[bytes_read] = '\0';
|
||||||
|
printf("Received: %s\n", buffer);
|
||||||
|
}
|
||||||
|
free(buffer);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
11
client_handler.h
Normal file
11
client_handler.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef CLIENT_HANDLER_H
|
||||||
|
#define CLIENT_HANDLER_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
void* client_handler(void* args);
|
||||||
|
|
||||||
|
#endif
|
||||||
10
webserver.c
10
webserver.c
@@ -1,9 +1,11 @@
|
|||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <pthread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
// #include <pthread.h>
|
|
||||||
|
#include "client_handler.h"
|
||||||
|
|
||||||
struct sockaddr_in server_addr;
|
struct sockaddr_in server_addr;
|
||||||
|
|
||||||
@@ -43,8 +45,10 @@ int main(int argc, char **argv) {
|
|||||||
if (client < 0) {
|
if (client < 0) {
|
||||||
perror("Failed to accept client");
|
perror("Failed to accept client");
|
||||||
continue;
|
continue;
|
||||||
} else {
|
|
||||||
printf("Client connected\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pthread_t thread;
|
||||||
|
pthread_create(&thread, NULL, client_handler, (void *)client);
|
||||||
|
pthread_detach(thread);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user