From 4e9b368a7281cc1ff9dd1d43fbc3ac05c69a09f6 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 8 Jul 2021 00:10:36 +0200 Subject: [PATCH] WIP: [BUG] new network code --- src/cli.c | 169 ++++++++++++++++------------------------------- src/server.c | 181 ++++++++++++++++++++++++--------------------------- 2 files changed, 140 insertions(+), 210 deletions(-) diff --git a/src/cli.c b/src/cli.c index ad2938f..a967e87 100644 --- a/src/cli.c +++ b/src/cli.c @@ -23,135 +23,78 @@ #include #include +#include +#include + +#define BUFFERSIZE 80 #define SERVER_IP_ADDR "127.0.0.1" -#define SERVER_PORT 90190 +#define SERVER_PORT 9000 -// -------------------------------------------------------------------------- // -// Socket init function // -// -------------------------------------------------------------------------- // -static inline int createSocket(void) +void connectedCommunication(int sockfd) { - int socketDescriptor; - - //printLog("Create the socket\n"); - - socketDescriptor = socket(AF_INET, SOCK_STREAM, 0); - return socketDescriptor; -} - -static inline int connectSocket(int newSocket) -{ - int effectiveSocket = -1; - int serverPort = SERVER_PORT; - struct sockaddr_in remote = {0}; - - remote.sin_addr.s_addr = inet_addr(SERVER_IP_ADDR); - remote.sin_family = AF_INET; - remote.sin_port = htons(serverPort); - - effectiveSocket = - connect(newSocket, - (struct sockaddr*)&remote, - sizeof(struct sockaddr_in) - ); - - return effectiveSocket; -} - -static inline int sendSocket(int effectiveSocket, char *request, - short requestSize) -{ - int returnSocket = -1; - struct timeval tv; - - tv.tv_sec = 20; /* 20 Secs Timeout */ - tv.tv_usec = 0; - - if (setsockopt(effectiveSocket, - SOL_SOCKET,SO_SNDTIMEO, - (char *)&tv,sizeof(tv)) < 0) { - printLog("Time Out\n"); - return -1; - } - - returnSocket = send(effectiveSocket, request, requestSize, 0); - - return returnSocket; -} - -static inline int receiveSocket(int effectiveSocket, char *serverAnswer, - short receiveSize) -{ - int returnSocket = -1; - struct timeval tv; - - tv.tv_sec = 20; /* 20 Secs Timeout */ - tv.tv_usec = 0; - - if (setsockopt(effectiveSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, - sizeof(tv)) < 0) { - printLog("Time Out\n"); - return -1; - } - - returnSocket = recv(effectiveSocket, serverAnswer, receiveSize, 0); - //printLog("Anwer %s\n", serverAnswer); - - return returnSocket; -} - -int main(int argc, char **argv) -{ - int socketDescriptor; - - char clientMessage[255] = {0}; - char serverReply[255] = {0}; + char buff[BUFFERSIZE]; char *exitCommand = "exit"; + int n; - while(1) { + for (;;) { + // Zeroing buffer + bzero(buff, sizeof(buff)); printf("\ngem-graph console> "); - fgets(clientMessage, 255, stdin); - clientMessage[strcspn(clientMessage, "\n")] = '\0'; + n = 0; - if (strcmp(exitCommand,clientMessage) == 0) - { - close(socketDescriptor); - shutdown(socketDescriptor, 0); - shutdown(socketDescriptor, 1); - shutdown(socketDescriptor, 2); - return 0; + // Read command from terminal + while ((buff[n++] = getchar()) != '\n') + ; + + // Quit if asked + if (strcmp(exitCommand, buff) == 0) { + return; } - //Create socket - socketDescriptor = createSocket(); + // Otherwise send command to server + write(sockfd, buff, sizeof(buff)); - if(socketDescriptor == -1) { - printLog("Could not create socket\n"); - continue; - } + // Zeroing buffer + bzero(buff, sizeof(buff)); - //Connect to remote server - if (connectSocket(socketDescriptor) < 0) { - printLog("Connection failed\n"); - continue; - } + // Reading server answer + read(sockfd, buff, sizeof(buff)); - //Send data to the server - sendSocket(socketDescriptor, clientMessage, strlen(clientMessage)); + printf("%s\n", buff); + } +} - //Received the data from the server - receiveSocket(socketDescriptor, serverReply, 200); - printf("%s\n", serverReply); +int main() +{ + int sockfd; + struct sockaddr_in servaddr; - memset(serverReply, 0, 255); - - close(socketDescriptor); - shutdown(socketDescriptor, 0); - shutdown(socketDescriptor, 1); - shutdown(socketDescriptor, 2); + // Socket creation + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd == -1) { + printLog("Socket creation failed!\n"); + return 1; } - return 0; + bzero(&servaddr, sizeof(servaddr)); + + // Assign IP, PORT + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = inet_addr(SERVER_IP_ADDR); + servaddr.sin_port = htons(SERVER_PORT); + + // Connect to the server + if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0) { + printLog("Connection failed!\n"); + return 2; + } + + printLog("Connected!\n"); + + connectedCommunication(sockfd); + + // close the socket + close(sockfd); } diff --git a/src/server.c b/src/server.c index 2174667..1cd3fd2 100644 --- a/src/server.c +++ b/src/server.c @@ -22,8 +22,9 @@ #include "../include/base.h" #include "../include/cmds.h" +#include +#include #include -#include static void *serverMain(void *server); @@ -38,131 +39,117 @@ void ServerInit(Server_t *server) pthread_create(server->id, NULL, serverMain, server); } -// -------------------------------------------------------------------------- // -// Socket init function // -// -------------------------------------------------------------------------- // -static inline int createSocket(void) +#define BUFFER_SIZE 80 +static void serverCommunicationInstance(Server_t *args, int sockfd) { - int newSocket; - printLog("Create the socket\n"); - newSocket = socket(AF_INET, SOCK_STREAM, 0); - return newSocket; -} - -// -------------------------------------------------------------------------- // -// Soecket binding function // -// -------------------------------------------------------------------------- // -static inline int bindSocket(int newSocket) -{ - int effectiveSocket = -1; - int clientPort = 90190; - struct sockaddr_in remote = {0}; - - // Internet address family - remote.sin_family = AF_INET; - - // Any incoming interface - remote.sin_addr.s_addr = htonl(INADDR_ANY); - remote.sin_port = htons(clientPort); // Local port - effectiveSocket = bind(newSocket,(struct sockaddr*)&remote,sizeof(remote)); - - return effectiveSocket; -} - -// -------------------------------------------------------------------------- // -// Server main function // -// -------------------------------------------------------------------------- // -static void *serverMain(void *server) -{ - Server_t *args; - int socketDescriptor, effectiveSocket, clientRequestLength; - struct sockaddr_in client; - char clientRequest[255]= {0}; - char serverAnswer[255] = {0}; char **argv = NULL; - char *commandReturn; + char *buff, *commandReturn; int tokenIndex; - // Get args - args = (Server_t*) server; - printLog("Server #%lu online\n", *args->id); - - //Create socket - socketDescriptor = createSocket(); - - if (socketDescriptor < 0) { - printLog("Could not create socket\n"); - return NULL; - } - printLog("Socket created\n"); - - //Bind - if( bindSocket(socketDescriptor) < 0) { - //print the error serverAnswer - printLog("Socket bind failed\n"); - return NULL; - } - - printLog("Socket bind succeeded\n"); - - //Listen - listen(socketDescriptor, 3); - //Accept and incoming connection while(!args->pleaseStop) { + + // Zeroing buffer + bzero(buff, BUFFER_SIZE); printLog("Waiting for incoming connections...\n"); - clientRequestLength = sizeof(struct sockaddr_in); - //accept connection from an incoming client - effectiveSocket = accept(socketDescriptor,(struct sockaddr *)&client,(socklen_t*)&clientRequestLength); + // Read the message from client and copy it in buffer + read(sockfd, buff, sizeof(buff)); - if (effectiveSocket < 0) { - printLog("Acceptation failed\n"); - return NULL; - } - - printLog("Connection accepted\n"); - - memset(clientRequest, '\0', sizeof(clientRequest)); - strcpy(serverAnswer,"Invalid command\0"); - - //Receive REQUEST from client - if (recv(effectiveSocket, clientRequest, 200, 0) < 0) { - printLog("Reception failed"); - break; - } - - printLog("Client request : %s\n",clientRequest); + // Print buffer which contains the client request + printLog("Client request : %s\n", buff); // get args in an array tokenIndex = 0; argv = (char**) realloc(argv, 1 * sizeof(char*)); - argv[0] = strtok(clientRequest, " "); + argv[0] = strtok(buff, " "); while (argv[tokenIndex]) { tokenIndex++; argv = (char**) realloc(argv, (tokenIndex+1) * sizeof(char*)); argv[tokenIndex] = strtok(NULL, " "); } - // test first arg to find command in cmdList + // Zeroing buffer + bzero(buff, BUFFER_SIZE); + + // Execute command by first arg in cmdList for (int i = 0; i < LEN(cmdList); i++) { if (strcmp(cmdList[i].name, argv[0]) == 0) { commandReturn = cmdList[i].execute(argv, args); - strcpy(serverAnswer, commandReturn); + + // Copy server message in buffer + strcpy(buff, commandReturn); free(commandReturn); } } - // REPLY to client - if (send(effectiveSocket, serverAnswer, strlen(serverAnswer), 0) < 0) { - printLog("Send failed\n"); - return NULL; - } - - close(effectiveSocket); + // and send that buffer to client + write(sockfd, buff, sizeof(buff)); } free(argv); +} + +// -------------------------------------------------------------------------- // +// Server main function // +// -------------------------------------------------------------------------- // +#define PORT 9000 + +static void *serverMain(void *server) +{ + Server_t *args; + int sockfd, connfd; + uint len; + struct sockaddr_in servaddr, cli; + + // Get args + args = (Server_t*) server; + printLog("Server #%lu online\n", *args->id); + + // Create socket + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd == -1) { + printLog("Socket creation failed!\n"); + return NULL; + } + + // Prepare binding structure + bzero(&servaddr, sizeof(servaddr)); + + // Assign IP and PORT + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.sin_port = htons(PORT); + + // Binding newly created socket + if ((bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) != 0) { + printLog("Socket bind failed!\n"); + return NULL; + } + + // Now server is ready to listen and verification + if ((listen(sockfd, 5)) != 0) { + printLog("Socket listening failed!\n"); + return NULL; + } + + printLog("Server listening...\n"); + len = sizeof(cli); + + // Accept the data packet from client + connfd = accept(sockfd, (struct sockaddr*)&cli, &len); + if (connfd < 0) { + printLog("Server acccept failed!\n"); + return NULL; + } + printLog("Client accepted\n"); + + // Function for chatting between client and server + serverCommunicationInstance(args, connfd); + + // After communication ended close the socket + close(sockfd); + printLog("Server #%lu offline\n", *args->id); return NULL;