From 6f392c498d05d085418eb98009cd57d947256023 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Thu, 9 Feb 2023 19:41:12 +0100 Subject: [PATCH] fixed read_socket problems --- src/chat/server/main.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/chat/server/main.c b/src/chat/server/main.c index 843364e..fdf8ab2 100644 --- a/src/chat/server/main.c +++ b/src/chat/server/main.c @@ -18,6 +18,9 @@ along with this program. If not, see . #include +#include +#include + char *read_socket(int socket); int main(void) @@ -62,22 +65,17 @@ int main(void) char *read_socket(int socket) { - FILE *opened = why2_fdopen(socket, "r"); //OPEN socket long content_size; char *content; //COUNT content_size - fseek(opened, 0, SEEK_END); - content_size = ftell(opened); - rewind(opened); //REWIND + ioctl(socket, FIONREAD, &content_size); //ALLOCATE content = why2_calloc(content_size, sizeof(char)); - if (fread(content, content_size, 1, opened) != 1) why2_die("Reading socket failed!"); - - //DEALLOCATION - why2_deallocate(opened); + //READ + if (read(socket, content, content_size) != content_size) why2_die("Reading socket failed!"); return content; } \ No newline at end of file