moved server functions into why2-misc

This commit is contained in:
Václav Šmejkal 2023-02-21 10:53:09 +01:00
parent a51cfae3b0
commit 22ff5a3e7d
Signed by: ENGO150
GPG Key ID: F6D6DF86242C5A59
4 changed files with 66 additions and 65 deletions

View File

@ -20,5 +20,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#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

View File

@ -17,10 +17,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <why2/chat/common.h>
#include <why2/chat/misc.h>
#include <arpa/inet.h>
#include <why2/chat/misc.h>
int main(void)
{
int listen_socket = socket(AF_INET, SOCK_STREAM, 0); //CREATE SERVER SOCKET

View File

@ -18,11 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <why2/chat/common.h>
#include <unistd.h>
#include <time.h>
char *read_socket(int socket);
void *communicate_thread(void *arg);
#include <why2/chat/misc.h>
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;
}
}

View File

@ -18,8 +18,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <why2/chat/misc.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#include <why2/memory.h>
@ -41,4 +44,60 @@ void send_socket(char *text, int socket)
send(socket, final, text_length + 2, 0);
why2_deallocate(final);
}
}
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;
}