working on the web side of things
This commit is contained in:
@@ -17,6 +17,46 @@
|
||||
#include <arpa/inet.h>
|
||||
|
||||
|
||||
void handleClient(int sock) {
|
||||
while (1) {
|
||||
int client_sock = accept(sock, NULL, NULL);
|
||||
if (client_sock < 0) {
|
||||
perror("accept");
|
||||
continue;
|
||||
}
|
||||
|
||||
FILE* file = fopen("./web/index.html", "r");
|
||||
if (file == NULL) {
|
||||
perror("that file dose not exists");
|
||||
return;
|
||||
}
|
||||
|
||||
char* fileBuffer;
|
||||
|
||||
fseek(file, 0, SEEK_END);
|
||||
int size = ftell(file);
|
||||
rewind(file);
|
||||
printf("file size: %d\n", size);
|
||||
|
||||
fileBuffer = (char*)malloc(size + 512);
|
||||
printf("Sent '%s\n' to the client.\n", fileBuffer);
|
||||
|
||||
int bytesRead = fread(fileBuffer, sizeof(char), size, file);
|
||||
|
||||
char* response = (char*)malloc(512 + size); // Assuming a maximum length of 512 bytes
|
||||
|
||||
HttpResponseHeader* header = createHttpResponse(200, "text/html", fileBuffer, response, 512 + size);
|
||||
|
||||
fclose(file);
|
||||
|
||||
printf("%s\n", response);
|
||||
|
||||
|
||||
send(client_sock, response, strlen(response)+1, 0);
|
||||
free(fileBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock < 0) {
|
||||
@@ -37,28 +77,10 @@ int main() {
|
||||
listen(sock, 3);
|
||||
printf("Server started. Listening on port 9090.\n");
|
||||
|
||||
while (1) {
|
||||
int client_sock = accept(sock, NULL, NULL);
|
||||
if (client_sock < 0) {
|
||||
perror("accept");
|
||||
continue;
|
||||
}
|
||||
|
||||
char* response = (char*)malloc(512); // Assuming a maximum length of 512 bytes
|
||||
|
||||
HttpResponseHeader* header = createHttpResponse(200, HTTP_CONTENT_TYPE_HTML,
|
||||
"<h1>This is a test to see what happends</h1>", response, 512);
|
||||
|
||||
|
||||
printf("%s\n", response);
|
||||
|
||||
|
||||
printf("Sent 'Hello, World!' to the client.\n");
|
||||
|
||||
send(client_sock, response, strlen(response)+1, 0);
|
||||
|
||||
}
|
||||
handleClient(sock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user