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 "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();

View File

@ -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!");