diff --git a/include/chat/misc.h b/include/chat/misc.h index dbb4587..f2cdb02 100644 --- a/include/chat/misc.h +++ b/include/chat/misc.h @@ -20,5 +20,7 @@ along with this program. If not, see . #define WHY2_CHAT_MISC_H void send_socket(char *text, int socket); //send socket.... wtf did you expect +char *read_socket(int socket); //read lol +void *communicate_thread(void *arg); //COMMUNICATION THREAD #endif \ No newline at end of file diff --git a/src/chat/main/client.c b/src/chat/main/client.c index 797fc17..b739f97 100644 --- a/src/chat/main/client.c +++ b/src/chat/main/client.c @@ -17,10 +17,10 @@ along with this program. If not, see . */ #include -#include - #include +#include + int main(void) { int listen_socket = socket(AF_INET, SOCK_STREAM, 0); //CREATE SERVER SOCKET diff --git a/src/chat/main/server.c b/src/chat/main/server.c index 3efb3bf..2cab26e 100644 --- a/src/chat/main/server.c +++ b/src/chat/main/server.c @@ -18,11 +18,7 @@ along with this program. If not, see . #include -#include -#include - -char *read_socket(int socket); -void *communicate_thread(void *arg); +#include int main(void) { @@ -55,60 +51,4 @@ int main(void) } return 0; -} - -void *communicate_thread(void *arg) -{ - printf("User connected.\t%d\n", *((int*) arg)); - - const time_t startTime = time(NULL); - char *received = NULL; - - while (time(NULL) - startTime < 86400) //KEEP COMMUNICATION ALIVE FOR 24 HOURS - { - received = read_socket(*((int*) arg)); //READ - - if (received == NULL) return NULL; //FAILED; EXIT THREAD - - if (strcmp(received, "!exit\n") == 0) break; //USER REQUESTED PROGRAM EXIT - - printf("Received:\n%s\n\n", received); - - why2_deallocate(received); - } - - printf("User exited.\t%d\n", *((int*) arg)); - - //DEALLOCATION - close(*((int*) arg)); - why2_deallocate(received); - - return NULL; -} - -char *read_socket(int socket) -{ - if (socket == -1) - { - fprintf(stderr, "Reading socket failed.\n"); - return NULL; - } - - unsigned short content_size = 0; - char *content_buffer = why2_calloc(2, sizeof(char)); - - //GET LENGTH - if (recv(socket, content_buffer, 2, 0) != 2) return NULL; - - content_size = (unsigned short) (((unsigned) content_buffer[1] << 8) | content_buffer[0]); - - why2_deallocate(content_buffer); - - //ALLOCATE - content_buffer = why2_calloc(content_size + 1, sizeof(char)); - - //READ FINAL MESSAGE - if (recv(socket, content_buffer, content_size, 0) != content_size) fprintf(stderr, "Socket probably read wrongly!\n"); - - return content_buffer; -} +} \ No newline at end of file diff --git a/src/chat/misc.c b/src/chat/misc.c index 9007451..57b4c3a 100644 --- a/src/chat/misc.c +++ b/src/chat/misc.c @@ -18,8 +18,11 @@ along with this program. If not, see . #include +#include #include #include +#include +#include #include @@ -41,4 +44,60 @@ void send_socket(char *text, int socket) send(socket, final, text_length + 2, 0); why2_deallocate(final); -} \ No newline at end of file +} + +void *communicate_thread(void *arg) +{ + printf("User connected.\t%d\n", *((int*) arg)); + + const time_t startTime = time(NULL); + char *received = NULL; + + while (time(NULL) - startTime < 86400) //KEEP COMMUNICATION ALIVE FOR 24 HOURS + { + received = read_socket(*((int*) arg)); //READ + + if (received == NULL) return NULL; //FAILED; EXIT THREAD + + if (strcmp(received, "!exit\n") == 0) break; //USER REQUESTED PROGRAM EXIT + + printf("Received:\n%s\n\n", received); + + why2_deallocate(received); + } + + printf("User exited.\t%d\n", *((int*) arg)); + + //DEALLOCATION + close(*((int*) arg)); + why2_deallocate(received); + + return NULL; +} + +char *read_socket(int socket) +{ + if (socket == -1) + { + fprintf(stderr, "Reading socket failed.\n"); + return NULL; + } + + unsigned short content_size = 0; + char *content_buffer = why2_calloc(2, sizeof(char)); + + //GET LENGTH + if (recv(socket, content_buffer, 2, 0) != 2) return NULL; + + content_size = (unsigned short) (((unsigned) content_buffer[1] << 8) | content_buffer[0]); + + why2_deallocate(content_buffer); + + //ALLOCATE + content_buffer = why2_calloc(content_size + 1, sizeof(char)); + + //READ FINAL MESSAGE + if (recv(socket, content_buffer, content_size, 0) != content_size) fprintf(stderr, "Socket probably read wrongly!\n"); + + return content_buffer; +}