Multiple clients !
This commit is contained in:
parent
48ca39577a
commit
428ac5ff4f
|
@ -122,11 +122,13 @@ struct {
|
||||||
struct {
|
struct {
|
||||||
pthread_t id;
|
pthread_t id;
|
||||||
bool pleaseStop;
|
bool pleaseStop;
|
||||||
|
int sockfd;
|
||||||
} typedef Server_t;
|
} typedef Server_t;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
pthread_t id;
|
pthread_t id;
|
||||||
bool pleaseStop;
|
bool pleaseStop;
|
||||||
|
Server_t *parent;
|
||||||
int sockfd;
|
int sockfd;
|
||||||
} typedef ServerCommunication_t;
|
} typedef ServerCommunication_t;
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,8 @@
|
||||||
#include "../include/scheduler.h"
|
#include "../include/scheduler.h"
|
||||||
#include "../include/model.h"
|
#include "../include/model.h"
|
||||||
|
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
#define LINE_LENGTH 80
|
#define LINE_LENGTH 80
|
||||||
#define LINE_NUMBER 24
|
#define LINE_NUMBER 24
|
||||||
|
|
||||||
|
|
73
src/server.c
73
src/server.c
|
@ -25,6 +25,8 @@
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
static void *serverMain(void *server);
|
static void *serverMain(void *server);
|
||||||
|
|
||||||
|
@ -40,13 +42,16 @@ void ServerInit(Server_t *server)
|
||||||
|
|
||||||
#define SEND_BUFFER_SIZE 80 * 24
|
#define SEND_BUFFER_SIZE 80 * 24
|
||||||
#define RECEIVE_BUFFER_SIZE 80
|
#define RECEIVE_BUFFER_SIZE 80
|
||||||
static void serverCommunicationInstance(Server_t *args, int sockfd)
|
void *serverCommunicationInstance(void *server)
|
||||||
{
|
{
|
||||||
|
ServerCommunication_t *args;
|
||||||
char **argv = NULL;
|
char **argv = NULL;
|
||||||
char receiveBuff[RECEIVE_BUFFER_SIZE];
|
char receiveBuff[RECEIVE_BUFFER_SIZE];
|
||||||
char sendBuff[SEND_BUFFER_SIZE];
|
char sendBuff[SEND_BUFFER_SIZE];
|
||||||
int tokenIndex;
|
int tokenIndex;
|
||||||
|
|
||||||
|
args = (ServerCommunication_t*) server;
|
||||||
|
|
||||||
//Accept and incoming connection
|
//Accept and incoming connection
|
||||||
while(!args->pleaseStop) {
|
while(!args->pleaseStop) {
|
||||||
|
|
||||||
|
@ -55,7 +60,7 @@ static void serverCommunicationInstance(Server_t *args, int sockfd)
|
||||||
printLog("Waiting for commands...\n");
|
printLog("Waiting for commands...\n");
|
||||||
|
|
||||||
// Read the message from client and copy it in buffer
|
// Read the message from client and copy it in buffer
|
||||||
read(sockfd, receiveBuff, sizeof(receiveBuff));
|
read(args->sockfd, receiveBuff, sizeof(receiveBuff));
|
||||||
|
|
||||||
// Ignore null-sized request
|
// Ignore null-sized request
|
||||||
if (receiveBuff[0] == 0)
|
if (receiveBuff[0] == 0)
|
||||||
|
@ -80,16 +85,18 @@ static void serverCommunicationInstance(Server_t *args, int sockfd)
|
||||||
// Execute command by first arg in cmdList
|
// Execute command by first arg in cmdList
|
||||||
for (int i = 0; i < LEN(cmdList); i++) {
|
for (int i = 0; i < LEN(cmdList); i++) {
|
||||||
if (strcmp(cmdList[i].name, argv[0]) == 0) {
|
if (strcmp(cmdList[i].name, argv[0]) == 0) {
|
||||||
cmdList[i].execute(sendBuff, argv, args);
|
cmdList[i].execute(sendBuff, argv, args->parent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// and send that buffer to client
|
// and send that buffer to client
|
||||||
write(sockfd, sendBuff, sizeof(sendBuff));
|
write(args->sockfd, sendBuff, sizeof(sendBuff));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
close(args->sockfd);
|
||||||
printLog("End of communications \n");
|
printLog("End of communications \n");
|
||||||
free(argv);
|
free(argv);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------------- //
|
// -------------------------------------------------------------------------- //
|
||||||
|
@ -100,7 +107,8 @@ static void serverCommunicationInstance(Server_t *args, int sockfd)
|
||||||
static void *serverMain(void *server)
|
static void *serverMain(void *server)
|
||||||
{
|
{
|
||||||
Server_t *args;
|
Server_t *args;
|
||||||
int sockfd, connfd;
|
ServerCommunication_t serverSlots[50] = {0};
|
||||||
|
int connfd, flags, serverSlotIndex = 0;
|
||||||
uint len;
|
uint len;
|
||||||
struct sockaddr_in servaddr, cli;
|
struct sockaddr_in servaddr, cli;
|
||||||
|
|
||||||
|
@ -109,12 +117,23 @@ static void *serverMain(void *server)
|
||||||
printLog("Server #%lu online\n", args->id);
|
printLog("Server #%lu online\n", args->id);
|
||||||
|
|
||||||
// Create socket
|
// Create socket
|
||||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
args->sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (sockfd == -1) {
|
if (args->sockfd == -1) {
|
||||||
printLog("Socket creation failed!\n");
|
printLog("Socket creation failed!\n");
|
||||||
goto serverExiting;
|
goto serverExiting;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
flags = fcntl(args->sockfd, F_GETFL);
|
||||||
|
if (flags == -1) {
|
||||||
|
printLog("Socket parameters getting failed!\n");
|
||||||
|
goto serverExiting;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fcntl(args->sockfd, F_SETFL, flags | O_NONBLOCK) < 0) {
|
||||||
|
printLog("Socket non-blocking setting failed!\n");
|
||||||
|
goto serverExiting;
|
||||||
|
}
|
||||||
|
|
||||||
// Prepare binding structure
|
// Prepare binding structure
|
||||||
bzero(&servaddr, sizeof(servaddr));
|
bzero(&servaddr, sizeof(servaddr));
|
||||||
|
|
||||||
|
@ -124,38 +143,48 @@ static void *serverMain(void *server)
|
||||||
servaddr.sin_port = htons(PORT);
|
servaddr.sin_port = htons(PORT);
|
||||||
|
|
||||||
// Binding newly created socket
|
// Binding newly created socket
|
||||||
if ((bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) != 0) {
|
if ((bind(args->sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) != 0) {
|
||||||
printLog("Socket bind failed!\n");
|
printLog("Socket bind failed!\n");
|
||||||
goto serverEarlyExiting;
|
close(args->sockfd);
|
||||||
|
goto serverExiting;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now server is ready to listen and verification
|
// Now server is ready to listen and verification
|
||||||
if ((listen(sockfd, 5)) != 0) {
|
if ((listen(args->sockfd, 5)) != 0) {
|
||||||
printLog("Socket listening failed!\n");
|
printLog("Socket listening failed!\n");
|
||||||
|
close(args->sockfd);
|
||||||
goto serverExiting;
|
goto serverExiting;
|
||||||
}
|
}
|
||||||
|
|
||||||
len = sizeof(cli);
|
len = sizeof(cli);
|
||||||
|
|
||||||
|
printLog("Server listening...\n");
|
||||||
|
|
||||||
while (!args->pleaseStop) {
|
while (!args->pleaseStop) {
|
||||||
printLog("Server listening...\n");
|
|
||||||
|
|
||||||
// Accept the data packet from client
|
// Accept the data packet from client
|
||||||
connfd = accept(sockfd, (struct sockaddr*)&cli, &len);
|
connfd = accept(args->sockfd, (struct sockaddr*)&cli, &len);
|
||||||
if (connfd < 0) {
|
if (connfd < 0) {
|
||||||
printLog("Server acccept failed!\n");
|
if (errno != EWOULDBLOCK) {
|
||||||
goto serverExiting;
|
printLog("Server acccept failed!\n");
|
||||||
|
goto serverExiting;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
printLog("Client accepted\n");
|
||||||
|
|
||||||
|
serverSlots[serverSlotIndex].sockfd = connfd;
|
||||||
|
serverSlots[serverSlotIndex].parent = args;
|
||||||
|
|
||||||
|
pthread_create(&serverSlots[serverSlotIndex].id,
|
||||||
|
NULL,
|
||||||
|
serverCommunicationInstance,
|
||||||
|
(void*)&serverSlots[serverSlotIndex]);
|
||||||
|
|
||||||
|
printLog("Server listening...\n");
|
||||||
}
|
}
|
||||||
printLog("Client accepted\n");
|
|
||||||
|
|
||||||
// Function for chatting between client and server
|
|
||||||
serverCommunicationInstance(args, connfd);
|
|
||||||
}
|
}
|
||||||
serverExiting:
|
|
||||||
// After communication ended close the socket
|
|
||||||
close(sockfd);
|
|
||||||
|
|
||||||
serverEarlyExiting:
|
serverExiting:
|
||||||
printLog("Server #%lu offline\n", args->id);
|
printLog("Server #%lu offline\n", args->id);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue