First command!

This commit is contained in:
Adrien Bourmault 2021-06-17 21:34:50 +02:00
parent 1256c65f62
commit ca1e959861
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
3 changed files with 23 additions and 39 deletions

View File

@ -69,6 +69,8 @@ struct {
#define LOGMSG "[%s]"
#define printLog(FORMAT, ...) printf(LOGMSG " " FORMAT, __func__, ##__VA_ARGS__)
#define LEN(x) (sizeof(x) / sizeof((x)[0]))
/* -------------------------------------------------------------------------- */
//
@ -114,12 +116,6 @@ struct {
// Server
//
struct {
const char *name;
void* (*func)(void*);
const char *help;
} typedef Command_t;
struct {
pthread_t *id;
bool pleaseStop;

View File

@ -70,11 +70,6 @@ static inline int bindSocket(int newSocket)
return effectiveSocket;
}
//
// Commands understood by the server
//
// TODO dict of commands
// -------------------------------------------------------------------------- //
// Server main function //
// -------------------------------------------------------------------------- //
@ -86,8 +81,6 @@ static void *serverMain(void *server)
char clientRequest[255]= {0};
char serverAnswer[255] = {0};
const char *pMessage = "hello";
// Get args
args = (Server_t*) server;
printLog("Server #%lu online\n", *args->id);
@ -95,16 +88,14 @@ static void *serverMain(void *server)
//Create socket
socketDescriptor = createSocket();
if (socketDescriptor < 0)
{
if (socketDescriptor < 0) {
printLog("Could not create socket\n");
return NULL;
}
printLog("Socket created\n");
//Bind
if( bindSocket(socketDescriptor) < 0)
{
if( bindSocket(socketDescriptor) < 0) {
//print the error serverAnswer
printLog("Socket bind failed\n");
return NULL;
@ -116,16 +107,14 @@ static void *serverMain(void *server)
listen(socketDescriptor, 3);
//Accept and incoming connection
while(!args->pleaseStop)
{
while(!args->pleaseStop) {
printLog("Waiting for incoming connections...\n");
clientLen = sizeof(struct sockaddr_in);
//accept connection from an incoming client
effectiveSocket = accept(socketDescriptor,(struct sockaddr *)&client,(socklen_t*)&clientLen);
if (effectiveSocket < 0)
{
if (effectiveSocket < 0) {
printLog("Acceptation failed\n");
return NULL;
}
@ -133,26 +122,25 @@ static void *serverMain(void *server)
printLog("Connection accepted\n");
memset(clientRequest, '\0', sizeof(clientRequest));
memset(serverAnswer, '\0', sizeof(serverAnswer));
strcpy(serverAnswer,"Invalid command\0");
//Receive a reply from the client
if (recv(effectiveSocket, clientRequest, 200, 0) < 0)
{
//Receive REQUEST from client
if (recv(effectiveSocket, clientRequest, 200, 0) < 0) {
printLog("Reception failed");
break;
}
printLog("Client reply : %s\n",clientRequest);
if (strcmp(pMessage,clientRequest) == 0)
{
strcpy(serverAnswer,"Hello there.");
printLog("Client request : %s\n",clientRequest);
for (int i = 0; i < LEN(cmdList); i++) {
if (strcmp(cmdList[i].name, clientRequest) == 0) {
cmdList[i].execute(NULL);
strcpy(serverAnswer,"OK\0");
}
else
{
strcpy(serverAnswer,"Invalid command");
}
// Send some data
if (send(effectiveSocket, serverAnswer, strlen(serverAnswer), 0) < 0)
{
// REPLY to client
if (send(effectiveSocket, serverAnswer, strlen(serverAnswer), 0) < 0) {
printLog("Send failed\n");
return NULL;
}