//=-------------------------------------------------------------------------=// // Server management module // // // // Copyright © 2021 The Gem-graph Project // // // // This file is part of gem-graph. // // // // This program is free software: you can redistribute it and/or modify // // it under the terms of the GNU Affero General Public License as // // published by the Free Software Foundation, either version 3 of the // // License, or (at your option) any later version. // // // // This program is distributed in the hope that it will be useful, // // but WITHOUT ANY WARRANTY; without even the implied warranty of // // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // // GNU Affero General Public License for more details. // // // // You should have received a copy of the GNU Affero General Public License // // along with this program. If not, see . // //=-------------------------------------------------------------------------=// #include "../include/base.h" #include #include #include #include #include #include static void *serverMain(void *server); /* -------------------------------------------------------------------------- */ // -------------------------------------------------------------------------- // // Scheduler init function // // -------------------------------------------------------------------------- // pthread_t *ServerInit(Server_t *scheduler) { scheduler->id = (pthread_t*) calloc(1, sizeof(pthread_t)); pthread_create(scheduler->id, NULL, serverMain, scheduler); return scheduler->id; } // -------------------------------------------------------------------------- // // Socket init function // // -------------------------------------------------------------------------- // static inline int createSocket(void) { int hSocket; printLog("Create the socket\n"); hSocket = socket(AF_INET, SOCK_STREAM, 0); return hSocket; } // -------------------------------------------------------------------------- // // Soecket binding function // // -------------------------------------------------------------------------- // static inline int bindSocket(int hSocket) { int iRetval=-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 */ iRetval = bind(hSocket,(struct sockaddr *)&remote,sizeof(remote)); return iRetval; } // -------------------------------------------------------------------------- // // Server main function // // -------------------------------------------------------------------------- // static void *serverMain(void *server) { Server_t *args; int socketDescriptor, sock, clientLen, readSize; struct sockaddr_in client; char clientMessage[255]= {0}; char message[255] = {0}; const char *pMessage = "hello world!\n"; // Get args args = (Server_t*) server; //Create socket socketDescriptor = createSocket(); if (socketDescriptor) { printLog("Could not create socket\n"); return NULL; } printLog("Socket created\n"); //Bind if( bindSocket(socketDescriptor) < 0) { //print the error message printLog("bind failed.\n"); return NULL; } printLog("bind done\n"); //Listen listen(socketDescriptor, 3); //Accept and incoming connection while(!args->pleaseStop) { printLog("Waiting for incoming connections...\n"); clientLen = sizeof(struct sockaddr_in); //accept connection from an incoming client sock = accept(socketDescriptor,(struct sockaddr *)&client,(socklen_t*)&clientLen); if (sock < 0) { printLog("accept failed"); return NULL; } printLog("Connection accepted\n"); memset(clientMessage, '\0', sizeof clientMessage); memset(message, '\0', sizeof message); //Receive a reply from the client if( recv(sock, clientMessage, 200, 0) < 0) { printLog("recv failed"); break; } printLog("Client reply : %s\n",clientMessage); if(strcmp(pMessage,clientMessage)==0) { strcpy(message,"Hi there !"); } else { strcpy(message,"Invalid Message !"); } // Send some data if( send(sock, message, strlen(message), 0) < 0) { printLog("Send failed"); return NULL; } close(sock); } return 0; }