Added main listening thread, fixed random bytes at end string.

This commit is contained in:
Max 2023-02-01 16:40:51 +01:00
parent 815ce0c6c4
commit 3ef62d8c6d
5 changed files with 62 additions and 20 deletions

View File

@ -15,12 +15,11 @@ int main(int argc, char *argv[])
signal(SIGINT, app_cleanup);
list_add("Socket server", "server", start_server);
list_add("Socket client", "client", clientstart);
int result;
printf(" _____ \n / ____| \n | | _____ ___ __ \n | | / _ \\ \\/ / '_ \\ \n | |___| __/> <| |_) |\n \\_____\\___/_/\\_\\ .__/ \n | | \n |_| \n");
// Check for argruments and if we can use them.
result = checkarg(argc, argv);
int result = checkarg(argc, argv);
if (result == 0)
return result;
// If the app does not get args we will display a small options menu.
@ -35,7 +34,7 @@ int checkarg(int argc, char* argv[])
struct exp_data* command = get_command(argv[1]);
if (command == NULL)
return -1;
command->func_ptr(NULL);
return command->func_ptr(NULL);
}
return -1;
}
@ -45,12 +44,14 @@ int handleoptions()
PRINT_LINE("Choose a option:");
print_list_items();
int index;
if (scanf("%d", &index) == 0)
char inputBuffer[16];
if (fgets(inputBuffer, 10, stdin) != inputBuffer)
{
TRACE_WARN("No valid option selected!");
TRACE_WARN("Could not get input!");
return -1;
}
index = atoi(inputBuffer);
struct exp_data* data = get_from_list(index);
if (data == NULL)
{

View File

@ -44,7 +44,7 @@ int clientstart()
{
PRINT_LINE("No data received!");
}
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);
for (;;)
@ -62,6 +62,8 @@ int clientstart()
}
void print_help()
{
PRINT_LINE("Usage:");
PRINT_LINE("send <string>");
PRINT_LINE("Info:");
PRINT_LINE("\n");
PRINT_LINE("h - Help menu");
PRINT_LINE("q - Quit");
}

View File

@ -13,11 +13,38 @@ bool b_listen = true;
char sName[64] = "Test server Linux\0";
struct net_data server_data;
int setup_server();
void* thread_listen_network();
void* thread_client_handler(void* arg);
int start_server()
{
PRINT_LINE("Starting socket server...");
PRINT_LINE("Starting server...");
int server_result = setup_server();
if (server_result != 0)
return server_result;
server_result = setup_server_thread(thread_listen_network);
if (server_result != 0)
return -1;
char inputBuffer[100];
for (;;)
{
if (fgets(inputBuffer, 90, stdin) != inputBuffer)
{
TRACE_WARN("Could not get input from stdin!");
continue;
}
TRACE_WARN("Not a valid option given!");
}
return 0;
}
int setup_server()
{
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (server_sockfd < 0)
{
@ -35,26 +62,28 @@ int start_server()
TRACE_ERROR("Cannot bind to socket!");
return -1;
}
return 0;
}
void* thread_listen_network()
{
while (b_listen)
{
PRINT_LINE("Waiting for a connection...");
int listen_result = listen(server_sockfd, 1);
if (listen_result == -1)
{
TRACE_ERROR("Could not listen. code: %i", errno);
return -1;
TRACE_WARN("Could not listen. code: %i", errno);
continue;
}
int clientfd = accept(server_sockfd, NULL, NULL);
if (clientfd == -1)
{
TRACE_ERROR("Could not accept client!");
TRACE_WARN("Could not accept client!");
continue;
}
if (init_thread(&clientfd, thread_client_handler) == -1)
TRACE_WARN("Could not establish connection with client!");
}
return 0;
}
// Function used in thread to handle the connected client.
@ -69,17 +98,15 @@ void* thread_client_handler(void* arg)
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;
//char buff_recv[10];
sendResult = writeToSock(client_fd, &sName, sNameSize);
if (sendResult == -1)
return NULL;
char buff[50];
// Wait for command data from client.
// Wait for data from client.
for(;;)
{
PRINT_LINE("Listening to client for data...");
int rec_result;
rec_result = recv(client_fd, buff, sizeof(buff), 0);
if (rec_result == -1)
@ -90,13 +117,12 @@ void* thread_client_handler(void* arg)
if (rec_result == 0)
{
PRINT_LINE("Client disconnected!");
PRINT_LINE("Client: %d disconnected!", client_fd);
break;
}
}
int close_result = close(client_fd);
PRINT_LINE("Close network result: %i", close_result);
return NULL;
}

View File

@ -2,6 +2,8 @@
#include "cexp.h"
#include "thread_manager.h"
pthread_t server_listen_thread;
int init_thread(int* clientfd, void *(*func) (void *))
{
pthread_t thread;
@ -11,7 +13,17 @@ int init_thread(int* clientfd, void *(*func) (void *))
TRACE_ERROR("Could not create thread!");
return thread_create_result;
}
PRINT_LINE("Init thread: %lu", thread);
PRINT_LINE("Client: %d accepted on thread: %lu", *clientfd, thread);
return 0;
}
int setup_server_thread(void *(*func) (void*))
{
int thread_create_result = pthread_create(&server_listen_thread, NULL, func, NULL);
if (thread_create_result != 0)
{
TRACE_ERROR("Cannot create server thread!");
return thread_create_result;
}
return 0;
}

View File

@ -4,5 +4,6 @@
#include "sockets.h"
int init_thread(int* clientfd, void *(*func) (void *));
int setup_server_thread(void *(*func) (void*));
#endif