From 9352c8d3b85fe37c5b074331d9105d669d032876 Mon Sep 17 00:00:00 2001 From: Max <51083570+DRdrProfessor@users.noreply.github.com> Date: Mon, 6 Mar 2023 19:48:26 +0100 Subject: [PATCH] Cleaning preparing for initial packet handling --- src/sockets/client.c | 11 ++++++++--- src/sockets/server.c | 24 +++++++----------------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/sockets/client.c b/src/sockets/client.c index 23d0bcd..49babee 100644 --- a/src/sockets/client.c +++ b/src/sockets/client.c @@ -7,15 +7,17 @@ #include #include "cexp.h" #include "sockets.h" +#include "communication.h" +int client_sockfd; +struct sockaddr_in client_sockaddr; void print_help(); int clientstart() { - int client_sockfd = socket(AF_INET, SOCK_STREAM, 0); + client_sockfd = socket(AF_INET, SOCK_STREAM, 0); - struct sockaddr_in client_sockaddr; client_sockaddr.sin_family = AF_INET; client_sockaddr.sin_port = htons(PORT_NUM); client_sockaddr.sin_addr.s_addr = INADDR_ANY; @@ -26,7 +28,7 @@ int clientstart() TRACE_ERROR("Could not connect!"); return -1; } - + /* ssize_t nextRecSize = 0; ssize_t recvRead = readFromSock(client_sockfd, &nextRecSize, sizeof(uint32_t)); if (recvRead == -1) @@ -46,7 +48,10 @@ int clientstart() } serverName[nextRecSize] = '\0'; // Add a null byte to the end of the buffer or else we get some random output at the end of the string. PRINT_LINE("Connected with: %s", serverName); + */ + + // Handle user input for (;;) { char c = getchar(); diff --git a/src/sockets/server.c b/src/sockets/server.c index 37938bf..94d559b 100644 --- a/src/sockets/server.c +++ b/src/sockets/server.c @@ -11,8 +11,7 @@ int server_sockfd; struct sockaddr_in server_sockaddr; bool b_listen = true; -char sName[64] = "Test server Linux\0"; -CommData* srvInitData = NULL; +char* srvName = "Linux test server"; void* srvInitBuffer = NULL; uint32_t totalBufferSize; @@ -96,23 +95,15 @@ void* thread_client_handler(void* arg) { int client_fd = *(int*)arg; - // Send data size to client. - ssize_t sNameSize = strlen(sName); - uint32_t sDataSize = htonl(sNameSize); - int sendResult = writeToSock(client_fd, &sDataSize, sizeof(uint32_t)); - if (sendResult == -1)// If we are not be able to send data, return so the thread can exit. - return NULL; - sendResult = writeToSock(client_fd, &sName, sNameSize); - if (sendResult == -1) - return NULL; - + // Send the initial package to the client. + send_initial_pack(client_fd); char buff[50]; // Wait for data from client. for(;;) { int rec_result; - rec_result = recv(client_fd, buff, sizeof(buff), 0); + rec_result = recv(client_fd, buff, 45, 0); if (rec_result == -1) { TRACE_WARN("Could not recieve data from client!"); @@ -132,7 +123,6 @@ void* thread_client_handler(void* arg) void send_initial_pack(int client) { - char* srvName = "Linux test server"; if (srvInitBuffer == NULL) { uint32_t srtLen = strlen(srvName) + 1; @@ -143,9 +133,9 @@ void send_initial_pack(int client) TRACE_ERROR("Could not allocate memory!"); return; } - *(char*)srvInitBuffer = htonl(srtLen); - *((uint32_t*)srvInitBuffer + 1) = htonl(serverInfo); - void* dataBuffer = ((char*)srvInitBuffer + sizeof(uint32_t) + sizeof(EMessageType)); + *(char*)srvInitBuffer = htonl(srtLen); // Name string lengt + *((uint32_t*)srvInitBuffer + 1) = htonl(serverInfo); // Package type + void* dataBuffer = ((char*)srvInitBuffer + sizeof(uint32_t) + sizeof(EMessageType)); // Push offset for string copy if (strcpy(dataBuffer, srvName) != dataBuffer) { TRACE_ERROR("Could not copy data to buffer!");