Commit 5e108d86 authored by Maxime POULAIN's avatar Maxime POULAIN
Browse files

fix server crashing at client exit

parent 09fff080
No preview for this file type
......@@ -10,8 +10,6 @@
#include "bool.h"
void start(int port) {
bool needToStop = false;
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
......@@ -44,21 +42,33 @@ void start(int port) {
exit(EXIT_FAILURE);
}
printf("Listening on port %d\n", port);
while (true) {
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
while (!needToStop) {
printf("Client connected\n");
while (true) {
memset(bufferIn, 0, BUFFER_SIZE);
memset(bufferOut, 0, BUFFER_SIZE);
recv(new_socket, bufferIn, BUFFER_SIZE, 0);
if(strcmp(bufferIn,"exit") == 0){
break;
}
handleClientRequest(bufferIn, bufferOut);
printf("CLIENT : %s\n",bufferIn);
printf("SERVER : %s\n",bufferOut);
printf("CLIENT : %s\n", bufferIn);
printf("SERVER : %s\n", bufferOut);
send(new_socket, bufferOut, BUFFER_SIZE, 0);
}
printf("Client disconnected\n\n");
close(new_socket);
}
close(server_fd);
}
void handleClientRequest(char *bufferIn, char *bufferOut) {
......@@ -74,7 +84,7 @@ void handleClientRequest(char *bufferIn, char *bufferOut) {
}
void handleClientRequestSize(char *bufferIn, char *bufferOut) {
int len = strlen(bufferIn)-2;
int len = strlen(bufferIn) - 2;
sprintf(bufferOut, "%d", len);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment