Cleaning preparing for initial packet handling

This commit is contained in:
Max 2023-03-06 19:48:26 +01:00
parent 18f923c6ae
commit 9352c8d3b8
2 changed files with 15 additions and 20 deletions

View File

@ -7,15 +7,17 @@
#include <netinet/in.h> #include <netinet/in.h>
#include "cexp.h" #include "cexp.h"
#include "sockets.h" #include "sockets.h"
#include "communication.h"
int client_sockfd;
struct sockaddr_in client_sockaddr;
void print_help(); void print_help();
int clientstart() 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_family = AF_INET;
client_sockaddr.sin_port = htons(PORT_NUM); client_sockaddr.sin_port = htons(PORT_NUM);
client_sockaddr.sin_addr.s_addr = INADDR_ANY; client_sockaddr.sin_addr.s_addr = INADDR_ANY;
@ -26,7 +28,7 @@ int clientstart()
TRACE_ERROR("Could not connect!"); TRACE_ERROR("Could not connect!");
return -1; return -1;
} }
/*
ssize_t nextRecSize = 0; ssize_t nextRecSize = 0;
ssize_t recvRead = readFromSock(client_sockfd, &nextRecSize, sizeof(uint32_t)); ssize_t recvRead = readFromSock(client_sockfd, &nextRecSize, sizeof(uint32_t));
if (recvRead == -1) 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. 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); PRINT_LINE("Connected with: %s", serverName);
*/
// Handle user input
for (;;) for (;;)
{ {
char c = getchar(); char c = getchar();

View File

@ -11,8 +11,7 @@
int server_sockfd; int server_sockfd;
struct sockaddr_in server_sockaddr; struct sockaddr_in server_sockaddr;
bool b_listen = true; bool b_listen = true;
char sName[64] = "Test server Linux\0"; char* srvName = "Linux test server";
CommData* srvInitData = NULL;
void* srvInitBuffer = NULL; void* srvInitBuffer = NULL;
uint32_t totalBufferSize; uint32_t totalBufferSize;
@ -96,23 +95,15 @@ void* thread_client_handler(void* arg)
{ {
int client_fd = *(int*)arg; int client_fd = *(int*)arg;
// Send data size to client. // Send the initial package to the client.
ssize_t sNameSize = strlen(sName); send_initial_pack(client_fd);
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;
char buff[50]; char buff[50];
// Wait for data from client. // Wait for data from client.
for(;;) for(;;)
{ {
int rec_result; int rec_result;
rec_result = recv(client_fd, buff, sizeof(buff), 0); rec_result = recv(client_fd, buff, 45, 0);
if (rec_result == -1) if (rec_result == -1)
{ {
TRACE_WARN("Could not recieve data from client!"); TRACE_WARN("Could not recieve data from client!");
@ -132,7 +123,6 @@ void* thread_client_handler(void* arg)
void send_initial_pack(int client) void send_initial_pack(int client)
{ {
char* srvName = "Linux test server";
if (srvInitBuffer == NULL) if (srvInitBuffer == NULL)
{ {
uint32_t srtLen = strlen(srvName) + 1; uint32_t srtLen = strlen(srvName) + 1;
@ -143,9 +133,9 @@ void send_initial_pack(int client)
TRACE_ERROR("Could not allocate memory!"); TRACE_ERROR("Could not allocate memory!");
return; return;
} }
*(char*)srvInitBuffer = htonl(srtLen); *(char*)srvInitBuffer = htonl(srtLen); // Name string lengt
*((uint32_t*)srvInitBuffer + 1) = htonl(serverInfo); *((uint32_t*)srvInitBuffer + 1) = htonl(serverInfo); // Package type
void* dataBuffer = ((char*)srvInitBuffer + sizeof(uint32_t) + sizeof(EMessageType)); void* dataBuffer = ((char*)srvInitBuffer + sizeof(uint32_t) + sizeof(EMessageType)); // Push offset for string copy
if (strcpy(dataBuffer, srvName) != dataBuffer) if (strcpy(dataBuffer, srvName) != dataBuffer)
{ {
TRACE_ERROR("Could not copy data to buffer!"); TRACE_ERROR("Could not copy data to buffer!");